Some issues with using sizeof() in template definitions

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I have two issues with VC++ (2005 Pro SP1), which might or might not be related, but both have to do with using the sizeof inside template definitions.

The first one just baffles me, it makes no sense at all. I was creating an allocator to be used with a std::map. When using sizeof inside my allocator, such as to assign to an enum value defined in the class, the compiler complains about errors in <xtree>. Here's the code:

-------------------------------------
#include <memory>
#include <map>

// my allocator, stripped to the bare minimum
template<class T> struct MyAlloc : public std::allocator<T>
{
typedef std::allocator<T> MyBase;

template<class U> struct rebind
{
typedef MyAlloc<U> other;
};

enum { foo = sizeof(T) };

MyAlloc() { }
MyAlloc(const MyAlloc & other) : MyBase(other) { }
template<class U> MyAlloc(const MyAlloc<U> & other) : MyBase(other) { }
};

int main()
{
std::map<int, int, std::less<int>, MyAlloc<std::pair<const int, int> > > m;
}
-------------------------------------

If I compile this, I get these errors:

1>main.cpp
1>c:\...\xtree(39) : error C2061: syntax error : identifier '_Genptr'
1> c:\...\main.cpp(14) : see reference to class template instantiation 'std::_Tree_nod<_Traits>::_Node' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<int,int,std::less<int>,MyAlloc<std::pair<const int,int>>,false>
1> ]
1> c:\...\xtree(35) : see reference to class template instantiation 'MyAlloc<T>' being compiled
1> with
1> [
1> T=std::_Tree_nod<std::_Tmap_traits<int,int,std::less<int>,MyAlloc<std::pair<const int,int>>,false>>::_Node
1> ]
1> c:\...\xtree(68) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<int,int,std::less<int>,MyAlloc<std::pair<const int,int>>,false>
1> ]
1> c:\...\xtree(94) : see reference to class template instantiation 'std::_Tree_ptr<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<int,int,std::less<int>,MyAlloc<std::pair<const int,int>>,false>
1> ]
1> c:\...\xtree(112) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<int,int,std::less<int>,MyAlloc<std::pair<const int,int>>,false>
1> ]
1> c:\...\map(82) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<int,int,std::less<int>,MyAlloc<std::pair<const int,int>>,false>
1> ]
1> c:\...\main.cpp(23) : see reference to class template instantiation 'std::map<_Kty,_Ty,_Pr,_Alloc>' being compiled
1> with
1> [
1> _Kty=int,
1> _Ty=int,
1> _Pr=std::less<int>,
1> _Alloc=MyAlloc<std::pair<const int,int>>
1> ]
1>c:\...\xtree(46) : error C2146: syntax error : missing ';' before identifier '_Left'
1>c:\...\xtree(46) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\...\xtree(46) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\...\xtree(47) : error C2146: syntax error : missing ';' before identifier '_Parent'
1>c:\...\xtree(47) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\...\xtree(47) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\...\xtree(48) : error C2146: syntax error : missing ';' before identifier '_Right'
1>c:\...\xtree(48) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\...\xtree(48) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

(I put the ellipses in the paths to keep things more readable).

The funny thing is, as soon as I remove the enum definition in MyAlloc, everything compiles fine. Also, if I do not use sizeof(T) to calculate the value for foo, it works fine as well.



My other issue occured when trying to call a memberfunction on an object when it exists, or do nothing otherwise. I was using SFINAE for that:


-------------------------------------
#include <iostream>

template<unsigned> struct MemberCheck
{
MemberCheck(int) { }
};

template<class T>
void callFoo(T * t, MemberCheck<sizeof(&T::foo)> = 0)
{
t->foo();
}

void callFoo(void *)
{
}

struct A
{
void foo() { std::cout << "foo()" << std::endl; }
};

struct B
{
};

int main()
{
A a;
B b;
callFoo(&a);
callFoo(&b);
}
-------------------------------------

This gives me the following error:
1>c:\...\main.cpp(31) : error C2070: 'overloaded-function': illegal sizeof operand


Comeau accepts both code snippets just fine. I haven't tested other compilers though... And there's nothing in the C++ standard saying that using sizeof in a template function declaration is prohibited.

Are these known issues, or should I file a bugreport at Microsoft Connect? And if so, are these related, or are they seperate?


Regards,

Sylvester Hesp


.



Relevant Pages

  • Pointer-to-member-function as template parameter - strange error, plus an ICE
    ... Here's some code that makes the VC8 compiler do interesting things. ... template <class C, typename Sig> ... template ... struct Func ...
    (microsoft.public.vc.language)
  • Re: Incomplete type as template argument
    ... "Victor Bazarov" wrote in message ... >> struct B ... >> Could someone please explain to me when the compiler actually does ... >> the template expansion, and is this method of using incomplete types ...
    (comp.lang.cpp)
  • Re: Origin of size_t? Curious.
    ... >>> If the size cannot be taken, then how can sizeof do ... >> contains a declaration of type that is too large, ... > My compiler accepts the oversized declaration, ... at the point of struct declaration. ...
    (comp.lang.c)
  • Re: typename and sizeof
    ... template ... I would expect a 'typename' to be necessary. ... The point is that 'sizeof' has two ...
    (microsoft.public.vc.language)
  • Re: size of pointer variables
    ... >> only zeroes part of the struct, ... rather than the underlaying object, you don't have to check all the ... Is this a function that is compiler built in? ... sizeof is calculated at compile time and it ain't a function at ...
    (comp.lang.c)