对std/boost移动的调用不明确

Ambiguous call to std/boost move

本文关键字:调用 不明确 移动 boost std      更新时间:2023-10-16

遇到这个不编译的代码:

#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;
}

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

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

这不应该编译吗?using namespace子句难道不应该让这一点变得明确吗?为什么编译器在这里选择boost::move作为可行的候选者?

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

这是因为ADL依赖于参数的查找(请参阅这个答案,它也与boost相关)。

问题是考虑名称空间boost是因为:

§3.4.2与参数相关的名称查找

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

因此,由于某些升压相关类型是std::pair的模板自变量,因此也考虑boost::move。(我不应该这样做,因为我不知道如何围绕这种模糊性编程)。

作为处处显式限定std::move的替代方案,您可以定义BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE,它只是将boost::move别名为std::move。这样,您的示例就成功编译了。