Public Group

# C++ Templates

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

## 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 on other sites
Quote:
 Original post by JemburulaSometimes 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 Jemburulawhich 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 on other sites
Quote:
Original post by snk_kid
Quote:
 Original post by JemburulaSometimes 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 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015743
×