Re: name lookup and unnamed namespaces
seanf schrieb:
Yuck. But I see from 3.4.2 that I can't get C into the namespace (for
ADL purposes) with 'typedef' or 'using'.
I'm stuck with C in the global namespace, so I suppose I'll make
operator<< static in the global namespace. I know this practice is
deprecated, but I don't expect they can remove it any time soon.
I hope I don't misquote the standard at this place (I would appreciate
the comments of others), but I don't think that you will have more
luck using internal linkage. I interpret 14.6.4.2, which says
"For a function call that depends on a template parameter, if the
function name is an unqualified-id but not a template-id, the
candidate functions are found using the usual lookup rules (3.4.1,
3.4.2) except that:
- For the part of the lookup using unqualified name lookup (3.4.1),
only function declarations with external linkage from the template
definition context are found.
- For the part of the lookup using associated namespaces (3.4.2),
only function declarations with external linkage found in either the
template definition context or the template instantiation context are
found."
so, that
static ostream& operator()(ostream&, constC&);
should not be found.
If you really prefer only a locally valid streaming capability of
class C, you could use a "private" proxy class for this. I mean
along the lines of:
class C{}; // Global namespace
#include <ostream>
#include <iostream>
#include <iterator>
using namespace std;
namespace {
struct CProxy {
CProxy(const C& c): c(&c) {}
ostream& print(ostream& os) const {
// use os to stream member c
return os;
}
const C* c;
};
inline ostream& operator<<(ostream& os, const CProxy& p) {
return p.print(os);
}
}
int main()
{
ostream_iterator<CProxy> i(cout, "\n");
i = CProxy(C());
}
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]