【发布时间】:2015-08-18 23:08:06
【问题描述】:
我正在尝试在 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 工作,运算符>> 必须与 my_pair 在同一个命名空间中,所以 std。
这样做会导致未定义的行为,因为我会将函数添加到命名空间 std。
我想避免继承,如struct my_pair : std::pair<int, int>。
有什么办法可以解决这个问题?
我在 OS X 上使用 clang++-3.6。
【问题讨论】:
-
@KirilKirov “我想避免继承”。别名不是类型。这是一个别名
-
@sehe - o,我没有注意到它是
using,而不是真正的类型。我真的错过了一些东西:)
标签: c++ boost std argument-dependent-lookup