• Popular Now

• 13
• 15
• 27
• 9
• 9

Archived

This topic is now archived and is closed to further replies.

template with sizeof operator

This topic is 5683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

1) is the sizeof operator resolved at run time or compile time ? 2) if it is resolved at compile time ? how does the compiler resolve this ?

template<class T>
int myvar(T a)
{
return sizeof(T);
}


i have been programming C++ for quite a long time, but i am still not sure on this ? . is it valid ? Its my duty, to please that booty ! - John Shaft

Share on other sites
Because, at compile time, it makes copies of the template for each type of template parameter combination.

To demonstrate:

  // the template definitiontemplate<class T>int myvar(T a){  return sizeof(T);}// one use:long l = 0x12345678L;int a = myvar(l);//resolves to:int myvar(long a){  return sizeof(long);}// another use:double d = 1234.5678;int b = myvar(d);//resolves to:int myvar(double a){  return sizeof(double);}

Behold the power of cheese.. I mean.. er.. templates

Share on other sites
damn.

i have a memory alignment problem because of that damn sizeof operator. its ok anyway because i am using VC6 and expected it to have some kind of templating issue.

Its my duty, to please that booty ! - John Shaft