Jump to content
  • Advertisement
Sign in to follow this  
Aiwendil

Typename

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

Hi everyone, I've recently started learning about templates, and I have a question. I've seen templates declared as
template<typename T>
at times, and at other times it's declared as
template<class T>
What's the difference? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Thanks for the replies, that makes sense now.

One last thing. With
T::A *aObj

and
typename T::A* a6;


Would there be an error if T did not contain an A?

Share this post


Link to post
Share on other sites
Quote:
Original post by Aiwendil
Thanks for the replies, that makes sense now.

One last thing. With
T::A *aObj

and
typename T::A* a6;


Would there be an error if T did not contain an A?

Of course.

Their example about using class instead of typename is one of the more interesting cases, and one that can cause confusion.



The first example says to take the value of A within the scope T, and multiply it by aObj. It is a subtle error, and generally not what the programmer meant. The code will work if you meant to do this:

class T {
public:
const int A = 4;
};

If A is not a value, the code should not compile.



The second says to create a pointer of T::A, which must be a type.

class T {
public:
struct A { ... };
};

If A is not a type, the code should not compile.

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!