我正在尝试在std::pair<int, int>
上使用boost::lexical_cast
。
#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;
}
如果我理解正确,为了使ADL工作,operator>>必须与my_pair在同一个命名空间内,因此必须使用std。这样做会导致未定义的行为,因为我将向命名空间std添加函数。我想避免继承,例如struct my_pair : std::pair。如何解决这个问题?我正在使用OS X上的clang++-3.6。