Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stream operator of a class nested in a class template

Reply
Thread Tools

stream operator of a class nested in a class template

 
 
Ares Lagae
Guest
Posts: n/a
 
      10-20-2008
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:stream << const foo<int>::bar
std::cout << b << "\n";
^

1 error detected in the compilation of "ComeauTest.c".
 
Reply With Quote
 
 
 
 
Ares Lagae
Guest
Posts: n/a
 
      10-20-2008
On Oct 20, 4:05 pm, Victor Bazarov <v.Abaza...@comAcast.net> 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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      10-20-2008
On Oct 20, 4:44*pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Ares Lagae wrote:
> > On Oct 20, 4:05 pm, Victor Bazarov <v.Abaza...@comAcast.net> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class nested inside a template class as template function argument type claudiu C++ 3 04-01-2011 01:02 PM
Class nested inside a template class as template function argument type claudiu C Programming 2 04-01-2011 12:10 PM
problem with a function template returning a private nested class ofa class template ymost@hotmail.com C++ 2 12-28-2008 10:43 AM
Stream operator in namespace masks global stream operator mrstephengross C++ 3 05-10-2007 07:02 AM
operator-overloading of nested class inside a template class Gerhard Pfeiffer C++ 3 09-14-2006 06:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57