stream operator of a class nested in a class template

Discussion in 'C++' started by Ares Lagae, Oct 20, 2008.

  1. Ares Lagae

    Ares Lagae Guest

    How is an output stream operator of a class nested in a class template
    defined? The code fragment below does not compile. Maybe it's just me,
    but I don't see why it should not compile.

    Best regards,

    #include <iostream>

    template <typename T>
    struct foo
    {
    struct bar {};
    bar bar_;
    const bar& get_bar() const { return bar_; }
    };

    template <typename Ch, typename Tr, typename T>
    std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>&
    ostream, typename foo<T>::bar&)
    {
    return ostream;
    }

    int main()
    {
    foo<int> f;
    const foo<int>::bar& b = f.get_bar();
    std::cout << b << "\n";
    }

    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for
    ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing. All rights reserved.
    MODE:strict errors C++ C++0x_extensions

    "ComeauTest.c", line 21: error: no operator "<<" matches these
    operands
    operand types are: std::eek:stream << const foo<int>::bar
    std::cout << b << "\n";
    ^

    1 error detected in the compilation of "ComeauTest.c".
     
    Ares Lagae, Oct 20, 2008
    #1
    1. Advertising

  2. Ares Lagae

    Ares Lagae Guest

    On Oct 20, 4:05 pm, Victor Bazarov <> wrote:

    Thank you for your quick reply.

    > Your argument is declared non-const here, but you pass a const ref.


    I have added "const" but that did not change anything.

    > Also, this is a non-deducible context.


    So it seems. The call "operator<< <char, std::char_traits<char>,
    int>(std::cout, b)" works.

    Can you explain Why?

    Best regards,

    Ares
     
    Ares Lagae, Oct 20, 2008
    #2
    1. Advertising

  3. Ares Lagae

    James Kanze Guest

    On Oct 20, 4:44 pm, Victor Bazarov <> wrote:
    > Ares Lagae wrote:
    > > On Oct 20, 4:05 pm, Victor Bazarov <> wrote:


    > > Thank you for your quick reply.


    > >> Your argument is declared non-const here, but you pass a const ref.


    > > I have added "const" but that did not change anything.


    > >> Also, this is a non-deducible context.


    > > So it seems. The call "operator<< <char, std::char_traits<char>,
    > > int>(std::cout, b)" works.


    > > Can you explain Why?


    > Explain why what?  Why it's a non-deducible context?  I don't
    > know. Here, the boilerplate answer is "because the Standard
    > says so", but why it's so in the Standard...  There are
    > *probably* cases in which the compiler would have hard time
    > deciding and the Standard Committee did not want to impose the
    > "undue hardship" on the compiler implementors. But your guess
    > is probably as good as mine.  Try looking in the archives for
    > "non-deducible context".


    I wasn't present during the discussions concerning this
    particular point, but I can make some good guesses as to why it
    isn't deducible. In the general case, the only way to deduce it
    would be to try every possible instantiation, to see if one
    matches---until you've instantiated Outer<T>, you can't know
    what Outer<T>::Inner is. Given that the number of possible
    instantiations is infinite, the committee probably felt that
    this was asking a little too much of the implementors.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Oct 20, 2008
    #3
    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. Gerhard Pfeiffer
    Replies:
    3
    Views:
    438
    Victor Bazarov
    Sep 14, 2006
  2. mrstephengross
    Replies:
    3
    Views:
    414
    James Kanze
    May 10, 2007
  3. Replies:
    2
    Views:
    554
  4. claudiu
    Replies:
    2
    Views:
    310
    claudiu
    Apr 1, 2011
  5. claudiu
    Replies:
    3
    Views:
    508
    claudiu
    Apr 1, 2011
Loading...

Share This Page