c++ - ADL using std types fails to find operator -
the following code fails compile
namespace { using c = std::vector<std::string>; std::ostream& operator << (std::ostream& lhs, const c& rhs) { lhs << 5; return lhs; } } int main() { a::c f; std::cout << f; return 0; }
with error
error c2679 binary '<<': no operator found takes right-hand operand of type 'a::c' (or there no acceptable conversion)
obviously cant find << operator presumably due considering c class std namespace. there way ensure compiler finds operator or otherwise work around problem?
a::c
type alias, , aliases transparent. don't "remember" came from. when argument-dependent lookup , figure out associated namespaces are, consider associated namespaces of types - not alias got there. can't add associated namespaces existing types. specific associated namespace of f
(which of type std::vector<std::string>
) std
, doesn't have operator<<
associated it. since there's no operator<<
found using ordinary lookup, nor there 1 found using adl, call fails.
now, said can't add associated namespaces existing types. can of course create new types:
namespace { struct c : std::vector<std::string> { }; }
or:
namespace { // template parameters considered associated namespaces struct s : std::string { }; using c = std::vector<s>; }
Comments
Post a Comment