I am trying to use boost::lexical_cast
on a std::pair<int, int>
.
#include <iostream>
#include <utility>
#include <boost/lexical_cast.hpp>
namespace my
{
// When my_pair is a user defined type, this program compiles
// and runs without any problems.
// When declaring my_pair as an alias of std::pair<int, int>,
// it fails to compile
/*
struct my_pair
{
int first;
int second;
};
*/
using my_pair = std::pair<int, int>;
std::istream& operator>>(std::istream& stream, my_pair& pair)
{
stream >> pair.first;
stream >> std::skipws;
stream >> pair.second;
return stream;
}
}
int main()
{
my::my_pair p = boost::lexical_cast<my::my_pair>("10 10");
std::cout << p.first << " " << p.second << std::endl;
return 0;
}
If I understand right, in order to make ADL work, the operator>> has to be in the same namespace as my_pair, so std.
Doing so, would result in undefined behavior, because I would be adding functions to the namespace std.
I would like to avoid inheritance, as in struct my_pair : std::pair<int, int>
.
What could be the solution to this problem?
I am using clang++-3.6 on OS X.