Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Some issues with using sizeof() in template definitions

82 views
Skip to first unread message

Sylvester Hesp

unread,
Aug 8, 2007, 11:57:42 AM8/8/07
to
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


Alex Blekhman

unread,
Aug 8, 2007, 2:03:27 PM8/8/07
to
"Sylvester Hesp" wrote:
[...]

> 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:
> [...]

This looks like a bug indeed. At least, I cannot see
anything wrong with the code.

Interestingly, a couple of days ago there was a thread in
this NG with similar issue. See the answer of Igor
Tandetnik:

"C1001 ICE with .NET 2003 compiling pointer to member
functions"
http://groups.google.com/group/microsoft.public.vc.language/msg/7e3445d5a8c60e95

The footnote 172 implies that result of "address of member
function" expression is of undefined type.

Alex

Alex Blekhman

unread,
Aug 8, 2007, 2:24:40 PM8/8/07
to
"Alex Blekhman" wrote:
> Interestingly, a couple of days ago there was a thread in
> this NG with similar issue. See the answer of Igor
> Tandetnik:
>
> "C1001 ICE with .NET 2003 compiling pointer to member
> functions"
> http://groups.google.com/group/microsoft.public.vc.language/msg/7e3445d5a8c60e95
>
> The footnote 172 implies that result of "address of member
> function" expression is of undefined type.

Oh, forget it. It has nothing to do with your problem.
Sorry.

Alex

Ben Voigt [C++ MVP]

unread,
Aug 8, 2007, 3:32:13 PM8/8/07
to
> The footnote 172 implies that result of "address of member function"
> expression is of undefined type.

Only members of classes in namespace std.

>
> Alex


0 new messages