Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 不可移动C++;17唯一指针_C++_C++11_C++17_Unique Ptr - Fatal编程技术网

C++ 不可移动C++;17唯一指针

C++ 不可移动C++;17唯一指针,c++,c++11,c++17,unique-ptr,C++,C++11,C++17,Unique Ptr,我找到了这个答案。然而,当在编译器上在线试用时,这适用于C++11(std::movecompiler error),但对于C++17,我看到下面的std::move是成功的。编译器不应该在那一行抛出错误吗?另外,如果C++17中的某些语义发生了更改,那么在C++17及以后的版本中创建不可移动的唯一\u ptr的正确方法是什么 template <typename T> using scoped_ptr = const std::unique_ptr<T>; int m

我找到了这个答案。然而,当在编译器上在线试用时,这适用于C++11(
std::move
compiler error),但对于C++17,我看到下面的
std::move
是成功的。编译器不应该在那一行抛出错误吗?另外,如果C++17中的某些语义发生了更改,那么在C++17及以后的版本中创建不可移动的唯一\u ptr的正确方法是什么

template <typename T>
using scoped_ptr = const std::unique_ptr<T>;

int main()
{
    auto p = scoped_ptr<int>(new int(5));
    auto p2 = std::move(p); // should be error?
    std::cout << *p2 << std::endl; // 5
    return 0;
}
模板
使用scoped_ptr=const std::unique_ptr;
int main()
{
自动p=范围内的ptr(新整数(5));
自动p2=std::move(p);//应该是错误吗?

std::cout注意,
p
被声明为非引用类型,参数
scoped_ptr(新int(5))的
const
部分在类型推断中被忽略。那么
p
的类型推断结果是
std::unique_ptr
,而不是
const std::unique_ptr
(即,如您所预期的,范围为
的ptr

你想要的可能是

auto& p = scoped_ptr<int>(new int(5)); // p is of type const std::unique_ptr<int>& now
auto&p=scoped_ptr(new int(5));//p的类型为const std::unique_ptr&now

p
不是
const
。请参阅,以了解它是否按预期方式失败

auto
templatevoid foo(T)
那样推断。
T
从来不会被推断为
const
,也不是
auto p=

同时,
auto p=
行可以工作,因为您是在mode.in.模式下编译的。这是因为prvalue在17中的差异;有些人称之为差异保证省略

如果您想要固定的唯一ptr:

template<class T, class D>
struct immobile_ptr:private std::unique_ptr<T, D>{
  using unique_ptr<T>::operator*;
  using unique_ptr<T>::operator->;
  using unique_ptr<T>::get;
  using unique_ptr<T>::operator bool;
  // etc

  // manually forward some ctors, as using grabs some move ctors in this case
};
template<class T, class...Args>
immobile_ptr<T> make_immobile_ptr(Args&&...args); // todo
模板
结构不可移动\u ptr:private std::unique\u ptr{
使用唯一的_ptr::运算符*;
使用unique_ptr::operator->;
使用unique_ptr::get;
使用唯一的_ptr::运算符bool;
//等
//在本例中,使用“抓取”可以手动转发某些CTOR,就像使用“移动”CTOR一样
};
模板
不可移动\u ptr使不可移动\u ptr(Args&&…Args);//待办事项
另一种选择可能是用一艘固定的驱逐舰进行一次独特的ptr

template<class X>
struct nomove_destroy:std::destroy<T>{
  nomove_destroy(nomove_destroy&&)=delete;
  nomove_destroy()=default;
  nomove_destroy& operator=(nomove_destroy&&)=delete;
};
template<class T>
using nomove_ptr=std::unique_ptr<T,nomove_destroy<T>>;
模板
结构nomove_destroy:std::destroy{
nomove_destroy(nomove_destroy&&)=删除;
nomove_destroy()=默认值;
nomove_destroy&operator=(nomove_destroy&&)=删除;
};
模板
使用nomove_ptr=std::unique_ptr;

<>但我不确定这是否有效。

欢迎进入C++中的<强>类型演绎< /强>的世界。尝试< /P>
auto & p = scoped_ptr<int>(new int(5));
auto&p=scoped_ptr(新int(5));

auto&&p=scoped_ptr(新int(5));

相反,本课程可能会有所帮助:

auto&
没有将其设置为非引用类型
const std::unique\u ptr
,是吗?@ShadowRanger你的意思是
const std::unique\u ptr&
?以这种方式跨版本工作感谢建议的课程-我想我需要看一下这篇文章,并阅读来自现代EFF的相关材料Cc++ C++:如果我读正确,DELTER技巧应该会起作用。
auto && p = scoped_ptr<int>(new int(5));