Jump to content
  • Advertisement
Sign in to follow this  
Jemburula

C++ Templates

This topic is 4832 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

Hey everyone, I took a quick break from my first true game in which you attempt to urinate accurately into a toilet and as the levels go up the drunker you get which results in blurry vision, to ask something. Sometimes in C++ things just don't flow along with the style of the rest of the language, one of these things I think are templates. Yes I see their usefulness but their syntax isn't very pretty not that I can think of anything better however. Anyway: I am wondering when you type
template <class T>
int Regurgitate(T Unknown)
{
  // Code
};
when does T go out of scope? I've gotten into the habit of writing it something like this
template <class T> int Regurgitate(T Unknown)
{
  // Code
};
which just leads me to a rational conclusion that it dies when the function does. Am I right to assume so? Thanks for your time, much appreciated. -Jemburula

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Jemburula
Sometimes in C++ things just don't flow along with the style of the rest of the language, one of these things I think are templates. Yes I see their usefulness but their syntax isn't very pretty not that I can think of anything better however.


Not at all, it probably means you still have along way to go...

Quote:
Original post by Jemburula
which just leads me to a rational conclusion that it dies when the function does.

Am I right to assume so?


Yes, function parameters of a template function are no different to a non template function they are local to the function's scope and they are on the stack. You should also relize that you are passing by value there.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Quote:
Original post by Jemburula
Sometimes in C++ things just don't flow along with the style of the rest of the language, one of these things I think are templates. Yes I see their usefulness but their syntax isn't very pretty not that I can think of anything better however.


Not at all, it probably means you still have along way to go...


They actually remind me of the decrepit goto statements

Share this post


Link to post
Share on other sites
T, as a class, never goes out of scope, because class names are not scoped.. However, the instance of T, in this case unknown, is just like any other object passed as a parameter, if it is passed by value, one is created on going into the function (via copy constructor), and it is then destroyed on exiting the function. That the class of unknown isn't definite has nothing to do with it's creation or destruction, that follows the rules just like everything else.

Or were you talking about when you can no longer refer to T as a class? In that case, yes, it is when the declaration or definition that it is 'paired to' closes. So in your case, it is indeed when the function defintion is closed.

Hope this helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!