int main()
{
//instruction 1 -> OK
std::unique_ptr<int[]> sp2(new int[10]);
//instruction 2 ->error does not compile
std::shared_ptr<int[]> sp1(new int[10]);
}
当分配给智能指针的内存带有new []时,我正在阅读有关delete和delete []的信息。为什么带有std::unique_ptr
的指令1可以,而带有std::shared_ptr
的指令1不能?
GCC 7或更高版本将编译您在以下方面遇到问题的代码:https://godbolt.org/z/pTWWVG
您在评论中链接的“ cpp.sh”网站使用的GCC 4.9很老,并且是第一个声称支持C ++ 14的版本。