Define namespace::operator<< in chained expression

Hi,
Im trying to figure out a smoother way to solve this problem:

1
2
3
cout << "Value of enum: ";
My_namespace::operator<<(cout, d);
cout << '\n';


The code is working fine but it looks messy.

Is there any way to do this in a chained expression like the following:

 
cout << "Value of enum: " << My_namespace::<< d << cout << '\n';


Is there any way to assign different namespaces for operators (like operator<<) in a chained expression.

Thanks in advance, and I'm sorry if my poor language skill is shining through.
You shouldn't need to do that. Koenig lookup will handle the case where operator<< for type T will be found in the
namespace where T is defined without the need for explicit scoping. In other words,

1
2
3
4
5
6
7
8
9
10
namespace foo {
    struct bar {};

    std::ostream& operator<<( std::ostream&, const bar& ) { /* ... */ }
}

int main() {
    foo::bar b;
    std::cout << b << std::endl; // This should compile
}


You are right, the code compiles fine now. Thanks for clearing that out! And thanks alot for the effort.
Topic archived. No new replies allowed.