question about stream operator and namespaces
I have a class with a stream operator which works as expected when the
class definition is not defined within a namespace. However, it does
not work as expect when defined within a namespace.
I am using gcc 3.4.6.
I include a short example program that shows the issue. On my system,
when the below program is saved as foo.cpp, the following compilation
line works and the program runs:
g++ -DUSE_NAMESPACE=0 -o foo foo.cc && ./foo
However the following compilation line fails:
g++ -DUSE_NAMESPACE=1 -o foo foo.cc && ./foo
Any ideas what I'm doing wrong?
-russ
Here is program:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <inttypes.h>
#include <vector>
using namespace std;
#ifndef USE_NAMESPACE
# define USE_NAMESPACE 0
#endif
#if (USE_NAMESPACE)
namespace foo
{
template <class T>
class Wrapper
{
public:
Wrapper() {}
explicit Wrapper(T const& t) : _t(t) {}
T const& operator()() const { return _t; }
private:
T _t;
};
}
template <class T>
ostream& operator<<(ostream& os, foo::Wrapper<T> const& t) {
os << t(); return os;
}
typedef foo::Wrapper<uint32_t> Seqno;
#else
template <class T>
class Wrapper
{
public:
Wrapper() {}
explicit Wrapper(T const& t) : _t(t) {}
T const& operator()() const { return _t; }
private:
T _t;
};
template <class T>
ostream& operator<<(ostream& os, Wrapper<T> const& t) {
os << t(); return os;
}
typedef Wrapper<uint32_t> Seqno;
#endif
int main(int argc, char* argv[]) {
vector<Seqno> seqno;
seqno.push_back(Seqno(1));
seqno.push_back(Seqno(2));
copy(seqno.begin(), seqno.end(), ostream_iterator<Seqno>(cout, "\n"));
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]