1d 智能指针不适用于语法 (*)++
1d Smart Pointer doesn't work with syntax (*)++
我正在学习智能指针。所以,我有箭头代码,我不明白。代码如下所示。我在visualstudio2019中尝试了以下代码。为什么我不能使用增量运算符(*i(++,因为我可以使用i[0]++。
shared_ptr<int[]> foo(shared_ptr<int[]>i)
{
cout << i << 'n';
(*i)++; // doesn't but works for shared_ptr<int>i as argument
i[0]++; // works
*i += 1; // <-- doesn't work
i[0] += 1; // <--- works
return i;
}
int main(int argc)
{
auto start = chrono::high_resolution_clock::now();
// int T;
//cin >> T;
// while (T--)
// {
std::unique_ptr<int[]> pointer(new int[5]); // <-- can do it
for (int i = 0; i < 5; ++i) pointer[i] = i;
auto pointer3 = make_unique<int[] >(new int[5]); // <--- can't do it Okay i know that there is no overload but what's reason for not having ?
cout << pointer << 'n';
auto pointer1 = std::move(pointer);
cout << pointer1 << 'n';
auto shrd_ptr = foo(std::move(pointer1));
cout << shrd_ptr << 'n';
// }
auto end = chrono::high_resolution_clock::now();
cout << std::chrono::duration_cast<std::chrono::milliseconds>(end -start).count() <<'n';
return 0;
}
错误列表:
Error C2088 '++': illegal for class Error C2100 illegal indirection Error (active) E0349 no operator "*" matches these operands Error (active) E0349 no operator "*" matches these operands Error (active) E0042 no instance of overloaded fucntion "make_unique" matches the argument list
如果shared_ptr
存储了一个数组,则(并且仅限于此(shared_ptr::operator[]
提供对存储数组的各个元素的访问。
CCD_ 3提供对整个存储对象的访问。它是为所有类型的存储对象定义的。如果这是一个数组,那么这就是整个数组。
这是对标准C/C++数组/指针语义的有意偏离,其中*x
和x[0]
在定义上是一回事。shared_ptr
不会这样做,因为它的设计者不希望它这样做。
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 正确语法,用于统一初始化名称中带有空格的类型的临时,如unsigned int
- 模板函数仅适用于VS
- 如何在cmake中包含适用于g++或viceversa的库
- 适用于win32、linux、mac的POSIX C包装器
- WinDBG适用于从Visual Studio 2015保存的转储,但不适用于任务管理器。显示异常代码"not found"