Home Dashboard Directory Help
Search

C2070 when using sizeof in template by jia3ep_


Status: 

Closed
 as Won't Fix Help for as Won't Fix


1
0
Sign in
to vote
Type: Bug
ID: 332857
Opened: 3/13/2008 2:50:14 AM
Access Restriction: Public
0
Workaround(s)
view
1
User(s) can reproduce this bug

Description

Error C2070 occured when using sizeof in template function.
Details
Sign in to post a comment.
Posted by Microsoft on 3/20/2008 at 8:41 AM
Having looked at this some more I am npw certain that this falls into the category of issues that has become the now infamous Issue 339 on the Core Language Issues List of the C++ Standards Committee.

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#339

I see that the problem in this case is that while sizeof(P) is easy to calculate sizeof(&P::x) isn't - especially in our compiler where the size of a pointer-to-member differs depending on the kind of inheritance involved.

This is an issue we are extremely unlikely to address until Issue 339 is finally resolved. I would take a look at the example on in the Issue from the April 2003 meeting - in these cases you can almost always replace a function template with a class template. In fact if you look at Boost and other TMP libraries you'll see that they almost always use class templates.

One final point - just because GCC compiles some code doesn't mean it is correct. EDG, which is by far and away the most confromant compiler around, rejects your sizeof(&P::x) example - which makes me feel that it is indeed currently ill-formed.

Jonathan Caves
Visual C++ Compiler Team
Posted by jia3ep_ on 3/20/2008 at 3:27 AM
Non-deduced contex definition is correct only while talking about second function argument. But in my "Steps to Reproduce" there is only one template parameter - P (template<class P>) in function that have two arguments - p and b. The only template argument may be deduced from first function parameter.

My sample compiles in gcc version 4.1.3 with no error messages.

Sample from C++ standard (ISO/IEC FDIS 14882:1998(E)) 14.8.2.4/14 shows what I'm talking about.

Please review "Steps to Reproduce" one more time. Sample in comments only contionues "Steps to Reproduce" but with different error message.

To see that problem only in sizeof(&P::x) you can try to compile the following sample (sizeof(&P::x) replaced with sizeof(P) - it's compiles with no errors!):
template<int> struct TT {typedef int type;};
template<class P> bool Check_x(P p, typename TT<sizeof(P)>::type b = 0) { return p.x==0; }
struct P1 {int x; };
int main()
{
P1 p1 = {1};
Check_x(p1);

return 0;
}

Last sample obviously is not a workaround because it is not allow to detect is there x(small) of X(capital) member as required.
Posted by jia3ep_ on 3/20/2008 at 3:26 AM
Non-deduced contex definition is correct only while talking about second function argument. But in my "Steps to Reproduce" there is only one template parameter - P (template<class P>) in function that have two arguments - p and b. The only template argument may be deduced from first function parameter.

My sample compiles in gcc version 4.1.3 with no error messages.

Sample from C++ standard (ISO/IEC FDIS 14882:1998(E)) 14.8.2.4/14 shows what I'm talking about.

Please review "Steps to Reproduce" one more time. Sample in comments only contionues "Steps to Reproduce" but with different error message.

To see that problem only in sizeof(&P::x) you can try to compile the following sample (sizeof(&P::x) replaced with sizeof(P) - it's compiles with no errors!):
template<int> struct TT {typedef int type;};
template<class P> bool Check_x(P p, typename TT<sizeof(P)>::type b = 0) { return p.x==0; }
struct P1 {int x; };
int main()
{
P1 p1 = {1};
Check_x(p1);

return 0;
}

Last sample obviously is not a workaround because it is not allow to detect is there x(small) of X(capital) member as required.
Posted by Microsoft on 3/19/2008 at 9:46 AM
I am pretty certain that this is what C++ calls a non-deduced context and therefore the error is correct - other C++ compilers agree (though none of them agree on the error message). I would suggest trying to use a class template instead of a function template for this code like this.

Jonathan Caves
Visual C++ Compiler Team
Posted by Microsoft on 3/13/2008 at 8:53 PM
Thanks for your feedback.

We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.
These specialized experts will follow-up with your issue.

Thank you,
Visual Studio Product Team
Posted by jia3ep_ on 3/13/2008 at 2:55 AM
There was minimal code in bug description. The main idea to detect is there x(small) of X(capital) member in some structure. The following code shows that:

template<int> struct TT {typedef int type;};
template<class P> bool Check_x(P p, typename TT<sizeof(&P::x)>::type b = 0) { return true; }
template<class P> bool Check_x(P p, typename TT<sizeof(&P::X)>::type b = 0) { return false; }

struct P1 {int x; };
struct P2 {int X; };

int main()
{
    P1 p1 = {1};
    P2 p2 = {1};

    Check_x(p1); // must return true
    Check_x(p2); // must return false
    
    return 0;
}
Sign in to post a workaround.