Sign in to follow this  

Templates and Overloading

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

This topic is 3780 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this