Jump to content
  • Advertisement
Sign in to follow this  
Gink

Why is this typedefd?

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

In this sample implementation of auto_ptr, this is typedefd <template T> typedef T element_type; but element_type is never used by any part of the class, why is there a typedef for it?

Share this post


Link to post
Share on other sites
Advertisement
Auto_ptr, that's part of the Standard Library, isn't it? Anyway, a lot of times that sort of thing is done so as to allow for people rolling their own implementations while still making it possible for them to interface with the rest of the library code. For example, if you made your own auto_ptr, and didn't typedef it's template parameter with that name, you would (presumably) break some of the algorithms that deal with auto_ptrs, because they reference a type with that name. (Not that there necessarily are any of these, I actually have no idea [smile]) Anyway, it's to provide a standard. Or something.. Don't take my word for it, find someone more knowledgable, and bother them until they answer you! [grin]

Share this post


Link to post
Share on other sites
The reason is so that you can get the type from the class. Here is an example:
    typedef std::auto_ptr< Foo > MyAutoPtr;
...
Foo * pA;
MyAutoPtr::element_type * pB;

If MyAutoPtr ever changes (e.g. Foo -> Bar), then the declaration of pA must be fixed. The declaration of pB is always ok.


Also, suppose you have a template where the template parameter is an auto_ptr and your template needs to know what type the auto_ptr points to. element_type is the only solution. For example:
    template < typename A > // A must be std::auto_ptr<>
void foo ( A & a )
{
A::element_type * p = a.release();
...
}

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
element_type is the only solution. For example:
    template < typename A > // A must be std::auto_ptr<>
void foo ( A & a )
{
A::element_type * p = a.release();
...
}


The only problem with that code is element_type is type dependant on the template type parameter "A", in which case typename is need to distinguish between a nested type and a static member, i.e:


template < typename A >
void foo(A& a) {

typename A::element_type* p = a.release();

}


@Gink the main reason for nested typedefs is for when it comes to generic & template meta programming, for example knowing the actual type of which the "pointer" points to which may not be directly available from a template type parameter.

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!