Cookie
Cookie

Reputation: 12672

C++ stream second insertion operator

Is it possible to define a second insertion operator to have two modes of outputting a class? Say e.g. one that outputs all members and one that just outputs some basic unique identifier that is grep-able in a log? If so, is there an operator that is usually chosen? I would guess as analogy to << one might use <<< if that is legal?

Thanks

Upvotes: 2

Views: 291

Answers (6)

Tony Delroy
Tony Delroy

Reputation: 106136

If you want to output only the id, then the best idea is probably to provide a method to get the id in a type that's streamable (e.g. std::string id() const;). That's much more intuitive to other people working on the code than some strange operator use.

Your suggestion of <<< (it's not possible to create new operators in C++, but ignoring that for a moment) reveals that you're happy for there to be different code at the point of call. Therefore, the only benefit you'd get would be the saving of a few character's source code; it isn't worth the obfuscation.

By way of contrast, there are situations where you want the same streaming notation to invoke different behaviours, such as switching between id-only and full data, or different representations such as tag/value, CSV, XML, and binary. These alternatives are usually best communicated by either:

Upvotes: 3

stijn
stijn

Reputation: 35901

you can use operator | for instance. Another way of doing this is to define small tag classes for which the operator is overloaded; example (pretty simplistic but you get the point):

template< class T >
struct GrepTag
{
  GrepTag( const T& );
  T value;
}

template< class T >
Greptag< T > MakeGrepTag( const T& x )
{
  return GrepTag< T >( x ); 
}

template< class T >
MyClass& MyClass::operator << ( const GrepTag< T >& g )
{
  //output g.value here
}

MyClass() << MakeGrepTag( "text" );

Yet another way, more like the standard streams, is to use a tag as well but keep some state internally:

struct GrepTag
{
}

MyClass& MyClass::operator << ( const GrepTag& g )
{
  grepState = true;
}

template< class T >
MyClass& MyClass::operator << ( const T& )
{
  if( grepState )
  {
    //output special
    grepState = false;
  }
  else
  {
    //output normal
  }        
}

MyClass() << GrepTag() << "text";

Upvotes: 1

sbi
sbi

Reputation: 224089

There's no such thing already defined or in use by convention.

Also, you cannot define your own operators in C++, you have to use one of the ones already in the language and overloadable, and <<< isn't an operator in C++, so it is out anyway.

I'd strongly recommend you don't use some other operator for this. (See rule #1 here for a more thorough explanation.) If you have subtle differences between output operations, well-chosen functions names go a long way for making better code than unclear operators arbitrarily picked.

Upvotes: 3

Shlublu
Shlublu

Reputation: 11027

You cannot define your own operators in C++. You can only overload those that exist.

So I recomend not using an operator for outputting basic unique identifier grep-able in a log. This doesn't correspond to any existing operator role. Use a method instead, such as exportToLog().

Upvotes: 0

x13n
x13n

Reputation: 4153

There is no such operator as <<< in C++.

You are, however, free to implement, for example operator <(ostream&,Object&), which would do what you want. The problem is, code may get unreadable when you try to chain < and << together.

Upvotes: 2

Alexandre C.
Alexandre C.

Reputation: 56956

No. You can't define your own operators (<<< doesn't exist in C++). But you can define a id() method returning a string and output this.

Upvotes: 2

Related Questions