Jump to content
  • Advertisement
Sign in to follow this  
beebs1

Templates and Overloading

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

Hiya, I've overloaded the insertion << operator on a class, and I'm wondering whether it's possible to overload it more than once, for specialized types? Something like this:
class C
{
public:

    // I use this to insert all the types I need so far
    template < typename T >
    inline C& operator<< ( const T &t );

    // I would like to be able insert an enumerated value, and have this class do something depending on what it is, e.g.
    enum TYPE { T1 = 0, T2, T3 };

}

// usage:
void main()
{
    C foo;
    foo << C::T1;
}
Can anyone tell me if this is possible? Thanks for any help :) James.

Share this post


Link to post
Share on other sites
Advertisement
You may:

  1. Overload the operator for that value:

    C & operator << (C::TYPE);


  2. Specialize the template for that value:

    template<>
    C & operator << (const C::TYPE &);


I would advise the first, since template specialization tends to have some annoying lookup-related side-effects.

Share this post


Link to post
Share on other sites
Thanks, that's what I needed.

Edit:

If I can change the topic slightly, instead of making a new thread, does anyone know if class methods defined in the class' declaration are automatically inlined? And similarly, are method in inline (.inl) files automatically inlined?

I'm using the MSVC 2005 IDE with the Intel C++ compiler.

Thanks.

[Edited by - beebs1 on August 13, 2007 12:39:27 PM]

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!