最近我很惊讶std::unique_ptrSTL容器的元素是可以接受的,因为我认为这些元素需要提供以下功能(这个页面说的相同):
但是std::unique_ptr不能复制使它所拥有的指针由单个对象拥有,这与上述要求相矛盾.
标准是否改变了要求?如果是这样,有什么变化?也许可移动物体或可复制物体都足够了?我在网上搜索了自C++ 11以来需求是否发生了变化,但我找不到任何帮助我的页面...
我目前正在尝试在 C++20 下构建遗留代码库,我遇到了这样的事情:
\nsize_t someCount; // value comes from somewhere else\n\xe2\x80\xa6\nstd::vector<const char *[2]> keyValues(someCount);\nRun Code Online (Sandbox Code Playgroud)\n我不能轻易地将其更改为类似的内容,std::vector<std:array<const char *, 2>>因为它稍后会传递给我无法控制的某个 API。只要我不启用 C++20,上述令人厌恶的内容就可以在 Clang 和 GCC 甚至 MSVC 中正常编译,但它会在 C++20 中的 MSVC 中中断,正如您在Godbolt 中看到的那样。
我认为这与是否使用上述构造函数的DefaultInsertable要求有关(这实际上是标准强制执行的T唯一要求)。根据 cppreference(请参阅前面的链接),C++17 之前的 STL 实现使用放置 new 来默认构造元素,从 C++20 开始,用于类型。这可能会触发 MSVC 从 C++17 回归到 C++20。std::construct_atDefaultInsertable
该标准规定,DefaultInsertable如果该表达式格式良好,则为类型:
allocator_traits<A>::construct(m, p)\nRun Code Online (Sandbox Code Playgroud)\n所以就我而言,那就是:
\nconst char * dummy[2];\nusing Allocator = std::allocator<const char *[2]>;\nAllocator a;\n// …Run Code Online (Sandbox Code Playgroud) 以下代码不能用gcc 4.7.0编译(使用std = c ++ 11 -O3)
int n;
std::vector< int[4] > A;
A.resize(n);
Run Code Online (Sandbox Code Playgroud)
错误消息是长度,但最终
functional cast to array type ‘_ValueType {aka int[4]}‘
Run Code Online (Sandbox Code Playgroud)
它是否正确?还是应该编译?更重要的是,如何避免这个问题?(没有定义一个新的结构来保存int[4])
编辑:
如何用C++ 98解决问题?
这个简单的程序
#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 …Run Code Online (Sandbox Code Playgroud) 我想保留一个数字向量。我不确定数组向量是什么样子,但我想它看起来像这样:
std::vector<int[5]> myvector;
Run Code Online (Sandbox Code Playgroud)
然后像这样访问它:
myvector[3][4] = 3;
Run Code Online (Sandbox Code Playgroud)
奇怪的是(或者不是)我以前从未见过这种技术。有没有理由呢。这样的事情本质上是不常见的吗?或者将数组包装在类对象类型中会更好吗?
我想创建一个for循环,它将用c ++中的数据填充一堆数组.现在为了节省空间,将来再添加一个阵列,我就有了for循环.用于演示目的的每个数组称为Array#(#是一个数字)for循环的点是设置一个具有最大数组的常量,然后通过将i附加到数组名称的末尾来遍历每个数组填充.
例如在伪代码中:
for (i = 1; i < numberofarrays; i++)
{ fill (Array & i) with ("Array" & i & "-default.txt")}
Run Code Online (Sandbox Code Playgroud) 我浏览了不同的来源,无法找到有关固定大小数组矢量的任何有用信息.我想知道下面的代码是否能正常工作.
std::vector<std::array<myType, [3]> > myVectorOfArrays;
Run Code Online (Sandbox Code Playgroud) 这不编译:
vector<int[2]> v;
int p[2] = {1, 2};
v.push_back(p); //< compile error here
Run Code Online (Sandbox Code Playgroud)
什么是替代方案?我不想使用std::array.
std::vector自身的声明编译。这push_back是不编译的。