std :: make_unique <std :: thread>与lambda

尼古拉斯·霍尔特豪斯(Nicolas Holthaus)

我正在尝试在Visual Studio 2013上编译以下代码:

std::unique_ptr<std::thread> threadPtr;
threadPtr.reset(std::make_unique<std::thread>([&]
{
  //...

}));

失败并显示以下错误:

error C2664: 'void std::unique_ptr<std::thread,std::default_delete<_Ty>>::
    reset(std::thread *) throw()' : cannot convert argument 1 from 
    'std::unique_ptr<std::thread,std::default_delete<_Ty>>' to 'std::thread *'

当我std::make_unique在没有问题的其他地方使用时这似乎很奇怪但是,当我不使用std::make_unique而是使用new它时,它可以工作:

std::unique_ptr<std::thread> threadPtr;
threadPtr.reset(new std::thread([&]
{
  //...

}));

我在这里做错什么,还是这是编译器问题?

用户名

std::make_unique返回std::unique_ptr但是std::unique_ptr::reset需要一个指针。因此,您正在寻找的是:

std::unique_ptr<std::thread> threadPtr(std::make_unique<std::thread>([&]
{
  //...

}));

或者:

threadPtr.reset(std::make_unique<std::thread>([&]
{
  //...

}).release());

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

std :: make_unique对SFINAE友好吗?

来自分类Dev

GDI +对象的std :: make_unique

来自分类Dev

使用std :: make_unique <>进行分配

来自分类Dev

'make_unique'不是'std'的成员

来自分类Dev

std :: make_unique与新的位置

来自分类Dev

std :: make_unique,匿名名称空间和ODR

来自分类Dev

如何使std :: make_unique成为我班上的朋友

来自分类Dev

R值引用和std :: make_unique

来自分类Dev

为什么 std::make_unique 调用复制构造函数

来自分类Dev

lambda移动捕获为std :: thread

来自分类Dev

std :: make_unique和std :: unique_ptr与new之间的区别

来自分类Dev

std :: make_unique和std :: unique_ptr之间的区别与内部新

来自分类Dev

我如何将 std::make_unique<derived>() 转换为 std::unique_ptr<base>

来自分类Dev

std :: make_shared和std :: make_unique在幕后如何工作?

来自分类Dev

C ++ 11:具有std :: thread和lambda函数的Segfault

来自分类Dev

std :: vector的std :: thread后代

来自分类Dev

std :: make_unique可以与抽象接口一起使用吗?

来自分类Dev

Android NDK clang编译器找不到std :: make_unique

来自分类Dev

将std :: make_unique与自定义删除器一起使用

来自分类Dev

带有std :: make_unique的push_back或emplace_back

来自分类Dev

C ++ 11/14 make_unique std :: string的模棱两可的重载

来自分类Dev

使用std :: make_unique堆栈溢出,但不使用原始指针

来自分类Dev

分配数组时是否可以将参数传递给std :: make_unique()?

来自分类Dev

当包含std :: promise作为成员时,为什么结构的make_unique失败?

来自分类Dev

在派生类上使用 std::make_unique 和自定义删除器?

来自分类Dev

使用make_shared <std :: thread>创建shared_ptr <std :: thread>的实例

来自分类Dev

如何终止std :: thread?

来自分类Dev

如何使用std :: thread?

来自分类Dev

iOS上的std :: thread

Related 相关文章

  1. 1

    std :: make_unique对SFINAE友好吗?

  2. 2

    GDI +对象的std :: make_unique

  3. 3

    使用std :: make_unique <>进行分配

  4. 4

    'make_unique'不是'std'的成员

  5. 5

    std :: make_unique与新的位置

  6. 6

    std :: make_unique,匿名名称空间和ODR

  7. 7

    如何使std :: make_unique成为我班上的朋友

  8. 8

    R值引用和std :: make_unique

  9. 9

    为什么 std::make_unique 调用复制构造函数

  10. 10

    lambda移动捕获为std :: thread

  11. 11

    std :: make_unique和std :: unique_ptr与new之间的区别

  12. 12

    std :: make_unique和std :: unique_ptr之间的区别与内部新

  13. 13

    我如何将 std::make_unique<derived>() 转换为 std::unique_ptr<base>

  14. 14

    std :: make_shared和std :: make_unique在幕后如何工作?

  15. 15

    C ++ 11:具有std :: thread和lambda函数的Segfault

  16. 16

    std :: vector的std :: thread后代

  17. 17

    std :: make_unique可以与抽象接口一起使用吗?

  18. 18

    Android NDK clang编译器找不到std :: make_unique

  19. 19

    将std :: make_unique与自定义删除器一起使用

  20. 20

    带有std :: make_unique的push_back或emplace_back

  21. 21

    C ++ 11/14 make_unique std :: string的模棱两可的重载

  22. 22

    使用std :: make_unique堆栈溢出,但不使用原始指针

  23. 23

    分配数组时是否可以将参数传递给std :: make_unique()?

  24. 24

    当包含std :: promise作为成员时,为什么结构的make_unique失败?

  25. 25

    在派生类上使用 std::make_unique 和自定义删除器?

  26. 26

    使用make_shared <std :: thread>创建shared_ptr <std :: thread>的实例

  27. 27

    如何终止std :: thread?

  28. 28

    如何使用std :: thread?

  29. 29

    iOS上的std :: thread

热门标签

归档