在C ++中创建未初始化的std :: thread时出现错误C2280 [英] Error C2280 while creating a std::thread without initialization in C++

查看:337
本文介绍了在C ++中创建未初始化的std :: thread时出现错误C2280的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的类/结构中创建一个线程对象,但是不想在声明期间立即初始化。 Intead希望稍后再做。因此,如下面的代码片段所示,我收到了此编译错误。我该如何解决?


错误C2280:'std :: thread :: thread(const std :: thread&)':尝试
引用已删除的函数


我观察到导致此错误的行是:


  std :: thread t2; 


这是否意味着我们不能只声明一个std: :thread没有初始化?这与该线程中的答案相矛盾是否可能定义一个std :: thread并在以后初始化它?

  struct Abc 
{
int a;
std :: thread t2;
void mem1()
{
printf( mem1\n);
}

void mem2()
{
printf( mem2\n);
std :: thread t1(& Abc :: mem1,Abc());
t2.swap(t1);
printf(产生t1\n之后);
t2.join();
}
};

错误详细信息:



C:\程序文件(x86)\ Microsoft Visual Studio 12.0\VC\include\xmemory0(611):错误C2280:' std :: thread :: thread(const std :: thread&)':尝试引用已删除的函数1>

C:\Program Files(x86)\Microsoft Visual Studio
12.0\VC\include\thread(70):请参见 std :: thread :: thread的声明1>此诊断发生在编译器生成的
函数'Abc :: Abc(const Abc& ;)




解决方案

编译器错误告诉您您正在尝试复制具有成员 std :: thread 的结构 Abc 。这是不可能的,因为 std :: thread 无法复制。



如果您将发布整个错误消息包括它最初指向的代码(即实际上调用 Abc 的副本构造函数的代码行),我可以为您提供更完整的解决方案。






旧答案保留在下面,但我认为在确定特定问题的原因上是不正确的。关于编译器生成的move构造函数的争论本身仍然有效。



您的代码很好。编译器无法编译对 swap 的调用,在仅移动类型的情况下,该调用依赖于所涉及类型的move构造函数。您的 std :: thread 不会明确定义它,因此在这种情况下,一致性编译器将生成它。



< Visual Studio 2013不会自动生成移动构造函数。这似乎是他们可以解决的错误(通过为实现 std :: thread 的实现显式定义它,并可能为每个其他 std 类(模板))。确保将编译器更新为VS 2013.5(更新5),看看是否有帮助。



如果这样做不起作用,请获取更新版本的Visual Studio。不生成移动构造函数的问题已在VS 2015中修复。VS2017也已发布。就标准c ++而言,后者要好很多,也要严格得多,尽管它仍然无法编译很多基本的SFINAE。


I want to create a thread object in my class/struct but don't want to initialie immediately during declaration. Intead want to do it later. Hence, I did as in below code snippet, I'm getting this compilation error. How do I fix this ?

error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function

I observed the line causing this error is :

  std::thread t2;

Does it mean that we can't just declare a std::thread without initializing ? This contradicts the answer in this thread Is it possible to define an std::thread and initialize it later?

struct Abc
{
    int a;
    std::thread t2;
    void mem1()
    {
        printf("mem1\n");
    }

    void mem2()
    {
        printf("mem2\n");
        std::thread t1(&Abc::mem1, Abc());
        t2.swap(t1);
        printf("after spawning t1\n");
        t2.join();
    }
};

Error Details:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(611): error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function 1>
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\thread(70) : see declaration of 'std::thread::thread' 1> This diagnostic occurred in the compiler generated function 'Abc::Abc(const Abc &)

解决方案

The compiler error is telling you that you are trying to copy a struct Abc which has a member std::thread. This is not possible because std::thread cannot be copied.

If you would have posted the entire error message including the code that it first points to (i.e. the line of code actually invoking Abc's copy constructor), I could provide you with a more complete solution.


The old answer is kept below but I believe it is incorrect in defining the cause of your specific issue. The ramble about compiler-generated move constructors is still valid in its own right.

Your code is fine. The compiler is failing to compile the call to swap, which in the case of move-only types relies on the move constructor of the types involved. Your std::thread won't have it explicitly defined, so a conformant compiler will generate it in this case.

Visual Studio 2013 does not automatically generate move constructors. This seems like a bug they could have worked around (by explicitly defining it for their std::thread implementation, and probably every other std class (template)). Make sure you update your compiler to VS 2013.5 (update 5) and see if that helps.

If that does not work, get a newer version of Visual Studio. The problem of not generating move constructors is fixed in VS 2015. Also VS 2017 is out. The latter is a lot better and a lot stricter when it comes to standard c++, even though it still fails to compile lots of "basic" SFINAE.

这篇关于在C ++中创建未初始化的std :: thread时出现错误C2280的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆