对std/boost移动的调用不明确
Ambiguous call to std/boost move
遇到这个不编译的代码:
#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与参数相关的名称查找
- 。。。命名空间和类的集合完全由函数参数的类型(以及任何模板模板参数的命名空间)决定
因此,由于某些升压相关类型是std::pair
的模板自变量,因此也考虑boost::move
。(我不应该这样做,因为我不知道如何围绕这种模糊性编程)。
作为处处显式限定std::move
的替代方案,您可以定义BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE
,它只是将boost::move
别名为std::move
。这样,您的示例就成功编译了。
相关文章:
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 为什么调用不明确的 ctor 时没有编译时错误?
- G++ 错误:重载的"abs(无符号 int)"的调用不明确
- 如果存在具有不同参数的继承成员,为什么对 C++ 结构函数的调用不明确?
- 错误:重载'stoi(nlohmann::basic_json<>::value_type&)'的调用不明确
- 构建 castalia 时出错:重载函数的调用不明确
- 错误:重载的"abs(double)"调用不明确
- 结构构造函数调用不明确
- 错误:对重载函数的调用不明确
- 对重载'swap(char&, char&)'的调用不明确
- 对可变模板函数的调用不明确
- C++ 编译错误:重载的"<function_name>(char*&)"调用不明确
- VS2010中具有list_of的调用不明确
- 为什么这个运算符=调用不明确
- 为什么对重载函数的调用不明确
- C++中重载运算符函数对构造函数的调用不明确
- 为什么这些重载函数调用不明确
- 重载write(int)的调用不明确
- 带有重载r值引用函数的调用不明确
- 传递0而不是1时,sfinae调用不明确