Implicit cast to object that implements overloaded operator

Discussion in 'C++' started by pnsteiner@gmail.com, Oct 12, 2005.

  1. Guest

    i want to use the << operator defined for ostream with an object that
    itself knows nothing of the operator, but is castable to ostream&. it
    seems that C++ doesn't do implicit casts before invoking a custom
    operator.

    i've tested with visual c++ 7 and the comeau online compilers, both
    required an explicit cast. is there a way to hint the compiler at using
    the cast operator implicitely, or is this not part of the language at
    all? (i have a scenario where this would come in very handy, though i
    am not sure if i would like c++ to behave like this anyways :)

    #include <iostream>
    using namespace std;

    class test
    {
    public:
    operator ostream& ()
    {
    return cout;
    }
    };

    void test()
    {
    test t;

    t << "test\n"; // implicit cast doesn't compile
    ((ostream&)t) << "test\n"; // explicit cast works
    }
    , Oct 12, 2005
    #1
    1. Advertising

  2. Greg Guest

    wrote:
    > i want to use the << operator defined for ostream with an object that
    > itself knows nothing of the operator, but is castable to ostream&. it
    > seems that C++ doesn't do implicit casts before invoking a custom
    > operator.
    >
    > i've tested with visual c++ 7 and the comeau online compilers, both
    > required an explicit cast. is there a way to hint the compiler at using
    > the cast operator implicitely, or is this not part of the language at
    > all? (i have a scenario where this would come in very handy, though i
    > am not sure if i would like c++ to behave like this anyways :)
    >
    > #include <iostream>
    > using namespace std;
    >
    > class test
    > {
    > public:
    > operator ostream& ()
    > {
    > return cout;
    > }
    > };
    >
    > void test()
    > {
    > test t;
    >
    > t << "test\n"; // implicit cast doesn't compile
    > ((ostream&)t) << "test\n"; // explicit cast works
    > }


    Instead of having test implement an ostream conversion operator, why
    not simply write a global << operator for an ostream (on the left) and
    a test (on the right)?

    Greg
    Greg, Oct 12, 2005
    #2
    1. Advertising

  3. wrote:
    > i want to use the << operator defined for ostream with an object that
    > itself knows nothing of the operator, but is castable to ostream&. it
    > seems that C++ doesn't do implicit casts before invoking a custom
    > operator.


    Only if the operator is declared a member. The compiler is not required
    to apply all possible conversions to try to find which conversion would
    be necessary to _then_ resolve a member function call.

    > i've tested with visual c++ 7 and the comeau online compilers, both
    > required an explicit cast. is there a way to hint the compiler at using
    > the cast operator implicitely, or is this not part of the language at
    > all?


    There is no way to "hint".

    > (i have a scenario where this would come in very handy, though i
    > am not sure if i would like c++ to behave like this anyways :)
    >
    > #include <iostream>
    > using namespace std;
    >
    > class test
    > {
    > public:
    > operator ostream& ()
    > {
    > return cout;
    > }
    > };
    >
    > void test()
    > {
    > test t;
    >
    > t << "test\n"; // implicit cast doesn't compile
    > ((ostream&)t) << "test\n"; // explicit cast works
    > }


    Use what works.

    V
    Victor Bazarov, Oct 12, 2005
    #3
  4. "" wrote:
    >
    > i want to use the << operator defined for ostream with an object that
    > itself knows nothing of the operator, but is castable to ostream&. it
    > seems that C++ doesn't do implicit casts before invoking a custom
    > operator.
    >


    An operator is nothing else then an ordinary function with some strange syntax.
    So when your compiler sees.

    > test t;
    >
    > t << "test\n"; // implicit cast doesn't compile


    it transforms this into:

    t.operator<<( "test\n" );

    Then the compiler scans the class test, searching for a function operator<<.
    Finding none, the compiler searches the freestanding functions for a standalone

    .... operator<<( test&, const char* );

    Finding none, the compiler gives up and emits an error message.
    The problem with that is that the compiler doesn't try to cast an
    object to something else, in order to be able to come up with a function
    it can call. Either that object's class (or one of its base classes) has
    that function or it has not.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 12, 2005
    #4
  5. Guest

    thanks for the prompt answer. it's clear to me now why implicit cast
    doesn't make any sense here, thinking of operators being nothing more
    than fancy method invokation syntax...
    , Oct 12, 2005
    #5
  6. Guest

    because the given code is just a sample for my problem. i have a class
    with lots of global << operator definitions all over the place, and i
    would like to be able to use those on another (unrelated) class without
    redefining all those operators...
    , Oct 12, 2005
    #6
  7. wrote:
    > thanks for the prompt answer. it's clear to me now why implicit cast
    > doesn't make any sense here, thinking of operators being nothing more
    > than fancy method invokation syntax...


    Actually, if operator<< with 'ostream&' as LHS and 'const char*' as RHS
    *were* a non-member, the conversion would be considered. Take a look:

    struct A {
    void operator + (const char*);
    };

    void operator - (A&, const char*);

    // operator+ is a member, operator- is not

    struct B {
    operator A& ();
    };

    int main() {
    B b;
    b + "abc"; // error here
    b - "abc"; // _no_ error here
    }

    V
    Victor Bazarov, Oct 12, 2005
    #7
  8. Guest

    i see, very interesting. actually my problem was about the usage of
    non-member operator functions, i was just unknowingly using the member
    operator << defined in ostream& in my test program, not recognizing the
    difference between member and non-member operator function definitions.

    thanks alot!
    , Oct 12, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matt p
    Replies:
    3
    Views:
    458
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Nov 7, 2004
  2. John Goche
    Replies:
    2
    Views:
    331
    Frederick Gotham
    Sep 4, 2006
  3. Afshin
    Replies:
    0
    Views:
    282
    Afshin
    Aug 12, 2011
  4. Afshin
    Replies:
    0
    Views:
    282
    Afshin
    Aug 12, 2011
  5. Afshin
    Replies:
    7
    Views:
    408
    Victor Bazarov
    Aug 16, 2011
Loading...

Share This Page