Compiler oddness with << overload

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cedarmillxing215@gmail.com

    Compiler oddness with << overload

    The oddness is on the last line of code. This is stripped down as far
    as I could figure, and I provide several similar examples that work as
    expected. I have no idea whether this is a compiler bug or my not
    understanding how the << overload is chosen.

    -Chris

    --------------------

    #include <iostream>
    #include <sstream>

    ostringstream get()
    {
    return ostringstream() ;
    }

    class my_ostringstrea m : public ostringstream
    {
    public:
    static my_ostringstrea m get() { return my_ostringstrea m(); }
    ~my_ostringstre am() { cout << str() << endl; }
    };



    int main(int argc, char * argv[])
    {
    ostringstream var1;
    var1 << "text 1";
    cout << var1.str() << endl; // Output: text 1

    ostringstream var2("text 2");
    cout << var2.str() << endl; // Output: text 2

    ostringstream var3 = get();
    var3 << "text 3";
    cout << var3.str() << endl; // Output: text 3

    {
    my_ostringstrea m var4;
    var4 << "text 4";
    } // Output: text 4

    {
    my_ostringstrea m var5 = my_ostringstrea m::get();
    var5 << "text 5";
    } // Output: text 5

    {
    my_ostringstrea m::get() << "text 6"; // WTF, seems to be treated
    as a void *
    } // Output: 00417800
    }
  • James Kanze

    #2
    Re: Compiler oddness with &lt;&lt; overload

    On Jul 17, 10:05 am, "Alf P. Steinbach" <al...@start.no wrote:
    * cedarmillxing.. .@gmail.com:
    Thank you Alf, that helps me more forward a little. It is
    still very confusing though.
    The void * version
    ostream & operator<<(ostr eam &, const void *)
    is an inherited member while the char * version
    ostream & operator<<(ostr eam &, const char *)
    is a standalone function (a friend?).
    But why does this mean that the void * overload is selected only in
    case #6? It seems that either function could have been called.
    Quoting myself, "a temporary (rvalue) can't bind to reference
    to non-const formal argument".
    But you can call a non-const member function on a temporary.
    Which is why << void* is found, but << char* not.
    This applies to the stream object.
    It's probably too late for this round, but I wonder if it
    wouldn't be a good idea to require all operator<< to be
    non-members. At least the overload set would be consistent.
    (IMHO, it would be an even better idea to make operator<< a
    template member, with all of the actual instances
    specializations . Because it does make sense to do things like
    "std::ostringst ream() << ...". But of course, doing that would
    break considerable code: probably every C++ application in
    existance, in fact.)

    --
    James Kanze (GABI Software) email:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    Working...