Having a custom type in a namespace and the streaming-operator in a parent namespace, the compiler fails to find the operator in a boost::property_tree::ptree::get()
operation. Example:
#include <boost/property_tree/ptree.hpp>
namespace NS1 {
struct Foo { int foo; };
}
template<typename IStream>
IStream& operator>>(IStream& is, NS1::Foo& val) {
is >> val.foo;
return is;
}
namespace NS2 {
void bar() {
std::stringstream ss;
auto& is = static_cast<std::istream&>(ss);
NS1::Foo foo;
is >> foo; // line 1
}
}
int main() {
NS2::bar(); // line 2
boost::property_tree::ptree tree;
tree.get_value_optional<NS1::Foo>(); // line 3
}
In the example, the compiler fails to find the operator unless it is moved into the NS1
namespace. But just the call in line 2 without line 3 does compile! Before line 1 the bar()
function even casts the stream to the same type that boost
uses in the customize_stream::extract()
function that fails the lookup.
My main problem is: what does the call in line 3 do differently from the one in lines 2 that the lookup fails?
And no: I would really prefer not to move the streaming operator into the same namespace as the class.