Jump to content
  • Advertisement

Archived

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

jwalker

template with sizeof operator

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

If you intended to correct an error in the post then please contact us.

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 this post


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

To demonstrate:


  
// the template definition

template<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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!