Re: name lookup and unnamed namespaces
seanf wrote:
This code fails to compile with gcc 3.4.6 & msvc++7:
(can't find operator<<)
--------8<------------
#include <ostream>
#include <iostream>
#include <iterator>
using namespace std;
class C {};
namespace
{
ostream& operator<<(ostream& o, const C&)
{
return o;
}
}
int main()
{
ostream_iterator<C> i(cout, "\n");
i = C();
}
--------8<------------
However, it works if I put the operator<< in the global namespace.
What's going on here?
The use of the operator<< is in the template code for
ostream_iterator, not in your code. In this case, the
operator<< will doubtlessly be a dependant name lookup (he sais,
without even looking at the source code). Dependant name lookup
does the lookup in two separate contexts: the context where the
template was defined (and it can never find your operator
there), and using ADL in the instantiation context. The fact
that it uses ADL only in the instantiation context means that
only names in a dependant namespace of one of the arguments can
be found: here in std (because of the std::ostream argument) and
in the global namespace (because of your class C). Since your
operator is in neither, it isn't found.
While trying to distil a minimal example, I found that this compiles:
--------8<------------
namespace N
{
template <class T> struct K
{
void f(const T& t) { g(t); }
};
}
using namespace N;
class C {};
namespace
{
void g(const C&) {}
}
int main()
{
K<C> k;
k.f(C());
}
--------8<------------
I can't seem to bridge the gap between the two examples. What am I
overlooking?
The fact that in this second example, the function call is not
in a template. There is only one context for lookup, which is
where the function call is written. (Think of it for a minute.
The function call in ostream_iterator is in <iterator>. Your
operator hasn't been declared at all yet.) In this context,
names found by both normal lookup and ADL are considered; normal
lookup at the point of the call finds your function, and so it
works.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]