Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7217

Re: Overloading << to accept "multiple right parameters"

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Overloading << to accept "multiple right parameters"
Date 2011-06-29 09:14 +1200
Message-ID <96uuhqFfpqU2@mid.individual.net> (permalink)
References <Xns9F12D7FE582CAbubbachipsetone@130.133.4.11> <0qak07dol2c633voqef904crsv4ual05n3@4ax.com> <Xns9F12E9C2E153Fbubbachipsetone@130.133.4.11>

Show all headers | View raw


On 06/29/11 08:59 AM, Bubba wrote:
> Robert Wessel's log on stardate 28 lip 2011
>
>> The "multiple operator" thing is an interesting hack - it isn't
>> actually any such thing, it just looks that way.  What actually
>> happens with streams is that the overload for operator<<  *returns*
>> the stream, thus the next<<  has the stream as its left operand, just
>> like the prior<<.  So typically you have something like:
>>
>>    ostream&  operator<<  (ostream&  os, const string&  str);
>>
>> and when you write:
>>
>>    cout<<  s1<<s1;
>>
>> what happens is that "cout<<  s1" is evaluated first and effectively
>> does "ostream::operator<<(cout, s1)".  The return value of that is
>> cout, so the second insert is effectively "cout<<  s2", and executes
>> as "ostream::operator<<(cout, s2)".
>>
>> IOW, when you define your actual function, it would be something
>> like:
>>
>>    ostream&  ostream::operator<<  (ostream&  os, const string&  str)
>>    {
>>    /* do something with str */
>>    return os;
>>    }
>
> But I'm not in to stream at all. :)
>
> I want to overload operator<<  in a fashion that it can accept both string
> and short as parameter and it is implemented as I described above.
>
> For example,
>
> 		void operator<<  (const string&mPar) {
> 			mMap.insert (pair<string,short>(mPar,0));
>          }
>
> works if I write
>
> mVar1<<  "string1";
>
> However, in this case
>
> mVar1<<  "string1"<<  "string2";
>
> error C2296: '<<' : illegal, left operand has type 'void'
> error C2297: '<<' : illegal, right operand has type 'const char [8]'

Your operator has the wrong return type for chaining.  Here is a short 
example of some code I've use for std::vector.  Expanding for std::map 
shouldn't be too hard.

#include <vector>
#include <iostream>
#include <algorithm>

template <typename T>
std::vector<T>& operator<<( std::vector<T>& vector, T v )
{
   vector.push_back(v);
   return vector;
}

void print( int n ) { std::cout << n << std::endl; }

int main()
{
   std::vector<int> v;

   v << 1 << 2;

   std::for_each( v.begin(), v.end(), print );
}

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Overloading << to accept "multiple right parameters" Bubba <nickname@banelli.biz.invalid> - 2011-06-28 19:14 +0000
  Re: Overloading << to accept "multiple right parameters" Robert Wessel <robertwessel2@yahoo.com> - 2011-06-28 14:39 -0500
    Re: Overloading << to accept "multiple right parameters" Bubba <nickname@banelli.biz.invalid> - 2011-06-28 20:59 +0000
      Re: Overloading << to accept "multiple right parameters" Ian Collins <ian-news@hotmail.com> - 2011-06-29 09:14 +1200
  Re: Overloading << to accept "multiple right parameters" Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-29 00:23 +0200
    Re: Overloading << to accept "multiple right parameters" Bubba <nickname@banelli.biz.invalid> - 2011-06-29 08:28 +0000
      Re: Overloading << to accept "multiple right parameters" Paavo Helde <myfirstname@osa.pri.ee> - 2011-06-29 14:31 -0500

csiph-web