• ### Popular Now

• 12
• 12
• 9
• 10
• 13

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

## 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 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 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]