对std/boost移动的模糊调用

mfo*_*ini 6 c++ boost

碰到这个不编译的代码:

#include <boost/move/utility.hpp>
#include <utility>
#include <deque>
#include <map>
#include <vector>
#include <boost/date_time/posix_time/posix_time_types.hpp>

using namespace std;

int main() {
    typedef std::pair<int, std::deque<int>> FirstPair;
    typedef std::vector<FirstPair> VectorFirstPair;
    typedef std::pair<boost::posix_time::time_duration, VectorFirstPair> SecondPair;
    typedef std::map<boost::posix_time::time_duration, SecondPair> Map;
    Map mapInstance;
    SecondPair newElement = make_pair(boost::posix_time::not_a_date_time, VectorFirstPair());
    mapInstance.insert(make_pair(boost::posix_time::seconds(10), move(newElement))).first;
}
Run Code Online (Sandbox Code Playgroud)

这在使用boost 1.55(不是在boost 1.54上)的gcc 4.8.2上失败,并出现以下错误(此处为 ideone ):

test.cpp: In function ‘int main()’:
test.cpp:17:81: error: call of overloaded ‘move(SecondPair&)’ is ambiguous
     mapInstance.insert(make_pair(boost::posix_time::seconds(10), move(newElement))).first;
                                                                                 ^
test.cpp:17:81: note: candidates are:
In file included from /usr/include/c++/4.8/bits/stl_pair.h:59:0,
                 from /usr/include/c++/4.8/utility:70,
                 from /usr/include/boost/config/no_tr1/utility.hpp:21,
                 from /usr/include/boost/config/select_stdlib_config.hpp:37,
                 from /usr/include/boost/config.hpp:40,
                 from /usr/include/boost/move/detail/config_begin.hpp:10,
                 from /usr/include/boost/move/utility.hpp:17,
                 from test.cpp:1:
/usr/include/c++/4.8/bits/move.h:101:5: note: constexpr typename std::remove_reference< <template-parameter-1-1> >::type&& std::move(_Tp&&) [with _Tp = std::pair<boost::posix_time::time_duration, std::vector<std::pair<int, std::deque<int> > > >&; typename std::remove_reference< <template-parameter-1-1> >::type = std::pair<boost::posix_time::time_duration, std::vector<std::pair<int, std::deque<int> > > >]
     move(_Tp&& __t) noexcept
     ^
In file included from test.cpp:1:0:
/usr/include/boost/move/utility.hpp:138:55: note: typename boost::remove_reference<T>::type&& boost::move(T&&) [with T = std::pair<boost::posix_time::time_duration, std::vector<std::pair<int, std::deque<int> > > >&; typename boost::remove_reference<T>::type = std::pair<boost::posix_time::time_duration, std::vector<std::pair<int, std::deque<int> > > >]
          inline typename remove_reference<T>::type && move(T&& t) BOOST_NOEXCEPT
Run Code Online (Sandbox Code Playgroud)

这不应该编译吗?该using namespace条款不应该明确无误吗?为什么编译器boost::move在这里选择一个可行的候选者?

请注意,如果我删除所boost定义类型中的类型(用例如替换它们int),这不会导致任何错误.

Wor*_*der 5

这是由于ADL-依赖于参数的查找(请参见与boost相关的答案)。

问题在于,boost考虑了名称空间是因为:

第3.4.2节与参数有关的名称查找

  1. ...名称空间和类的集合完全由函数参数的类型(以及任何模板模板参数的名称空间)决定。...

因此,由于某些与boost相关的类型是的模板参数std::pairboost::move因此也要考虑。(恕我直言,这不应该是因为我不知道该如何解决这种歧义)。