这个不可复制的地图是合法的c++11吗?GCC 4.7和MSVS 2010允许.Clang 3.1不允许
Is this non-copyable map legal c++11? GCC 4.7 and MSVS 2010 allow it. Clang 3.1 does not
我创建了一个不可复制的映射,无法使用clang进行编译。由于clang意味着非常符合标准,我想知道我的代码是否合法。MSVS 2010和GCC 4.7编译此代码时没有警告或错误。
附完整代码:有问题的行是main
的最后一行。
MSVS 2010 需要删除= delete
#include <utility>
#include <iostream>
#include <map>
template<typename Key_t, typename Value_t, typename Compare_t = std::less<Key_t> >
class non_copyable_map : public std::map<Key_t,Value_t,Compare_t>
{
typedef std::map<Key_t,Value_t,Compare_t> BaseType;
public:
non_copyable_map() { }
non_copyable_map(non_copyable_map&& t) : BaseType(std::move(t)) {}
non_copyable_map& operator = (non_copyable_map&& t)
{
if ( this != &t )
{
std::swap<BaseType>(*this,t);
}
return *this;
}
private:
non_copyable_map(const non_copyable_map&) = delete;
non_copyable_map& operator = (const non_copyable_map&) = delete;
};
int main(int argc, char* argv[])
{
non_copyable_map<int, non_copyable_map<int, int> > nestedMap;
non_copyable_map<int,int> inner;
inner[3]=4;
nestedMap[2] = std::move(inner); // THIS LINE CAUSES CLANG PROBLEMS
}
使用clang++-mp-3.1 -std=c++0x -stdlib=libc++ MapOfMaps.cpp
时的错误消息为:
In file included from MapOfMaps.cpp:2:
In file included from /usr/include/c++/v1/iostream:40:
In file included from /usr/include/c++/v1/istream:156:
In file included from /usr/include/c++/v1/ostream:134:
In file included from /usr/include/c++/v1/bitset:118:
/usr/include/c++/v1/__bit_reference:26:26: error: no type named '__storage_type' in
'std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<const
int, int> > >'
typedef typename _C::__storage_type __storage_type;
~~~~~~~~~~~~~^~~~~~~~~~~~~~
MapOfMaps.cpp:21:25: note: in instantiation of template class
'std::__1::__bit_reference<std::__1::map<int, int, std::__1::less<int>,
std::__1::allocator<std::__1::pair<const int, int> > > >' requested here
std::swap<BaseType>(*this,t);
^
MapOfMaps.cpp:36:15: note: in instantiation of member function 'non_copyable_map<int, int,
std::__1::less<int> >::operator=' requested here
nestedMap[2] = std::move(inner);
^
In file included from MapOfMaps.cpp:2:
In file included from /usr/include/c++/v1/iostream:40:
In file included from /usr/include/c++/v1/istream:156:
In file included from /usr/include/c++/v1/ostream:134:
In file included from /usr/include/c++/v1/bitset:118:
/usr/include/c++/v1/__bit_reference:27:26: error: no type named '__storage_pointer' in
'std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<const
int, int> > >'
typedef typename _C::__storage_pointer __storage_pointer;
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/v1/__bit_reference:33:25: error: no type named '__self' in
'std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<const
int, int> > >'
friend typename _C::__self;
~~~~~~~~~~~~~^~~~~~
In file included from MapOfMaps.cpp:3:
In file included from /usr/include/c++/v1/map:338:
/usr/include/c++/v1/__tree:1291:14: error: overload resolution selected deleted operator '='
__pair3_ = _STD::move(__t.__pair3_);
~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/v1/__tree:1308:9: note: in instantiation of member function
'std::__1::__tree<std::__1::pair<int, int>, std::__1::__map_value_compare<int, int,
std::__1::less<int>, true>, std::__1::allocator<std::__1::pair<int, int> >
>::__move_assign' requested here
__move_assign(__t, true_type());
^
/usr/include/c++/v1/__tree:1353:5: note: in instantiation of member function
'std::__1::__tree<std::__1::pair<int, int>, std::__1::__map_value_compare<int, int,
std::__1::less<int>, true>, std::__1::allocator<std::__1::pair<int, int> >
>::__move_assign' requested here
__move_assign(__t, integral_constant<bool,
^
/usr/include/c++/v1/map:736:21: note: in instantiation of member function
'std::__1::__tree<std::__1::pair<int, int>, std::__1::__map_value_compare<int, int,
std::__1::less<int>, true>, std::__1::allocator<std::__1::pair<int, int> > >::operator='
requested here
__tree_ = _STD::move(__m.__tree_);
^
/usr/include/c++/v1/type_traits:2342:9: note: in instantiation of member function
'std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<const
int, int> > >::operator=' requested here
__x = _STD::move(__y);
^
MapOfMaps.cpp:21:5: note: in instantiation of function template specialization
'std::__1::swap<std::__1::map<int, int, std::__1::less<int>,
std::__1::allocator<std::__1::pair<const int, int> > > >' requested here
std::swap<BaseType>(*this,t);
^
MapOfMaps.cpp:36:15: note: in instantiation of member function 'non_copyable_map<int, int,
std::__1::less<int> >::operator=' requested here
nestedMap[2] = std::move(inner);
^
/usr/include/c++/v1/memory:1918:7: note: candidate function (the implicit copy assignment
operator) has been explicitly deleted
class __compressed_pair
^
4 errors generated.
这可能是一个clang或libc++错误(很可能是libc++)。我不会用工具复制你的症状。最新的libc++标头在这里。这有点像是在黑暗中拍摄的,但试试这个:
swap(static_cast<BaseType&>(*this),static_cast<BaseType&>(t));
看起来你在<__bit_reference>
:中与这个swap
纠缠在一起
template <class _Cp, class _Dp>
_LIBCPP_INLINE_VISIBILITY inline
void
swap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT
这与地图无关。它刚好在范围内。
相关文章:
- std::multimap<std::chrono::milliseconds, T>::rbegin 在 MSVS-13 中指向 end()?
- 无法在 Visual Studio 2010 中构建 Allegro C++ 程序
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- Visual Studio 2013,错误 MSB8020:找不到 Visual Studio 2010 的生成工具
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- 双线程应用比单线程应用慢 C++ (VC++ 2010 Express).如何解决?
- std::array initalization with Visual Studio 2010
- C++20 概念测试在 MSVS 16.5 中不起作用
- MSVC 2010 编译应用程序和 MSVC 2019 编译应用程序之间的行为差异
- 将代码从 32 位迁移到 64 位时出现问题 Visual Studio 2010
- 在 Visual Studio 2010(及更高版本)中定义包含或链接路径变量的位置
- boost::stacktrace on Windows MSVS 2017
- 在MSVS 2010中同时设置多个断点
- MSVS 2010 C++编译器和堆栈对齐问题
- 将 MSVS 2010 项目转换为 MSVS 2012 RC,但出现错误"The C++ standard doesn't provide a hash for this type"
- MSVS 2010 和 C++ 标准的可视化生成问题
- 这个不可复制的地图是合法的c++11吗?GCC 4.7和MSVS 2010允许.Clang 3.1不允许
- 使用dllimport接收的void指针[MSVS c++ 2010]
- 在MSVS 2008和MSVS 2010中使用预构建的Qt二进制文件
- 为什么MSVS 2010 MFC项目依赖于MSVS 2008的vcredist_x86.exe