Some issues with using sizeof() in template definitions
- From: Sylvester Hesp <s.hespNO@xxxxxxxxxxxx>
- Date: Wed, 08 Aug 2007 17:57:42 +0200
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
.
- Follow-Ups:
- Re: Some issues with using sizeof() in template definitions
- From: Alex Blekhman
- Re: Some issues with using sizeof() in template definitions
- Prev by Date: Re: Interesting warning I get
- Next by Date: Re: A non-blocking recv socket function?
- Previous by thread: Interesting warning I get
- Next by thread: Re: Some issues with using sizeof() in template definitions
- Index(es):
Relevant Pages
|