Fed*_*dor 4 c++ arrays stdvector visual-studio-2019
这个简单的程序
#include <vector>
int main()
{
using int3 = int[3];
std::vector<int3> vec( 2 );
}
Run Code Online (Sandbox Code Playgroud)
无法在最新的 Visual Studio 2019 16.10.0 中使用stdcpplatestswitch 进行编译,从而产生错误:
>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility(144,1): error C2440: 'return': cannot convert from 'int *' to '_Ty (*)'
1> with
1> [
1> _Ty=int [3]
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility(144,48): message : Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xmemory(707): message : see reference to function template instantiation '_Ty (*std::construct_at<_Objty,,void>(_Ty (*const )) noexcept)[3]' being compiled
1> with
1> [
1> _Ty=int [3],
1> _Objty=int [3]
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xmemory(1610): message : see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,>(_Alloc &,_Objty (*const ))' being compiled
1> with
1> [
1> _Alloc=std::allocator<int3>,
1> _Ty=int [3],
1> _Objty=int [3]
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xmemory(1611): message : see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,>(_Alloc &,_Objty (*const ))' being compiled
1> with
1> [
1> _Alloc=std::allocator<int3>,
1> _Ty=int [3],
1> _Objty=int [3]
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xmemory(1811): message : see reference to function template instantiation 'void std::_Uninitialized_backout_al<_Alloc>::_Emplace_back<>(void)' being compiled
1> with
1> [
1> _Alloc=std::allocator<int3>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xmemory(1811): message : see reference to function template instantiation 'void std::_Uninitialized_backout_al<_Alloc>::_Emplace_back<>(void)' being compiled
1> with
1> [
1> _Alloc=std::allocator<int3>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\vector(1648): message : see reference to function template instantiation 'int (*std::_Uninitialized_value_construct_n<std::allocator<int3>>(int (*)[3],unsigned __int64,_Alloc &))[3]' being compiled
1> with
1> [
1> _Alloc=std::allocator<int3>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\vector(1646): message : while compiling class template member function 'int (*std::vector<int3,std::allocator<int3>>::_Ufill(int (*)[3],const unsigned __int64,std::_Value_init_tag))[3]'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\vector(493): message : see reference to function template instantiation 'int (*std::vector<int3,std::allocator<int3>>::_Ufill(int (*)[3],const unsigned __int64,std::_Value_init_tag))[3]' being compiled
1>Test.cpp(271): message : see reference to class template instantiation 'std::vector<int3,std::allocator<int3>>' being compiled
1>Done building project "Project1.vcxproj" -- FAILED.
Run Code Online (Sandbox Code Playgroud)
该程序在以前版本的 Visual Studio 2019 16.9.5 以及 gcc 11 中编译良好,但在 clang 5 中也无法编译。std::vector of array 是标准允许的还是特定于实现的行为?
来自cppreference:
T - 元素的类型。
(直到 C++11)
T必须满足CopyAssignable和CopyConstructible的要求。
(自 C++11 起)(直到 C++17)
对元素施加的要求取决于对容器执行的实际操作。一般要求元素类型是完整类型并满足Erasable的要求,但很多成员函数提出了更严格的要求。
(自 C++17 起)
对元素施加的要求取决于对容器执行的实际操作。一般来说,要求元素类型满足Erasable的要求,但许多成员函数提出了更严格的要求。如果分配器满足分配器完整性要求,则可以使用不完整的元素类型实例化此容器(但不是其成员)。
C 数组不可复制分配,也不可复制构造。它们也是不可擦除的(请参见此处),即使可以擦除,大多数方法也需要更多。
std::array<int,3>您可以毫无问题地创建 的向量。
PS:原则上可以编写一个自定义分配器,允许您构造std::vectorc 数组(请参阅此处以及对该答案的评论)。不过,这主要是出于好奇,因为你的能力仍然相当有限。C++11 中取消了这些要求,但“许多成员函数提出了更严格的要求”。