简体   繁体   English

C ++ Struct无法进行POD测试

[英]C++ Struct fails POD test

I am writing code for a microcontroller, and need to be sure my struct is a POD. 我正在为微控制器编写代码,需要确保我的结构是POD。 I thought I can use a "member initializer lists" to initialize members, but this does not pass the "is_pod" test. 我以为我可以使用“成员初始化列表”来初始化成员,但这不会通过“is_pod”测试。 In example below, A is a POD, B and C are not. 在下面的示例中,A是POD,B和C不是。 Adding "D() = default;" 添加“D()=默认值;” to D seems to make it a POD. 到D似乎使它成为POD。 But, by doing this, I can not longer have a "member initializer lists"? 但是,通过这样做,我不能再有“成员初始化列表”? Is there a way for a structure to be a POD, and have a "member initializer lists"? 结构是否有办法成为POD,并有一个“成员初始化列表”?

#include <iostream>
#include <string>

struct A {
    int var1;
};

struct B {
    int var1;
    //B() = default;
    B() : var1(100) {}
};

struct C {
    int bar [10];
    C() : bar{0} {}
};

struct D {
    int var1;
    D() = default;
    //D(int x) : var1(x) {}
};


int main()
{
    std::cout << std::boolalpha;
    std::cout << "\nIs A a POD = " << std::is_pod<A>::value;
    std::cout << "\nIs B a POD = " << std::is_pod<B>::value;
    std::cout << "\nIs C a POD = " << std::is_pod<C>::value;
    std::cout << "\nIs tD a POD = " << std::is_pod<D>::value;
}

=== Update 1 === ===更新1 ===
Thanks for replies! 谢谢你的回复! So, seems like there is no way to initialize the member variables in the structure definition. 所以,似乎没有办法在结构定义中初始化成员变量。 The following works, but is not as elegant as having the initialization in the struct itself. 以下工作,但不如在struct本身进行初始化那样优雅。

typedef struct A_ {
int var1;
} A;

A a = {
    .var1 = 100
};

POD must be a trivial type , this requires: POD必须是一个微不足道的类型 ,这需要:

Has one or more default constructors , all of which are either trivial or deleted, and at least one of which is not deleted. 有一个或多个默认构造函数所有 默认构造函数 都是 平凡的或删除的,并且至少有一个未删除。

And a trivial constructor has a description of performing no action . 一个简单的构造函数有一个不执行任何操作的描述。 So " having the initialization in the struct itself " is explicitly against it. 所以“ 在结构本身中进行初始化 ”明确地反对它。

The hard requirements of your interest are: 您感兴趣的硬性要求是:

  • The constructor is not user-provided (ie, is implicitly-defined or defaulted on its first declaration) 构造函数不是用户提供的(即,在其第一个声明中隐式定义或默认)
  • Every non-static member of class type has a trivial default constructor is that it has to be default, all all members constructors have 类类型的每个非静态成员都有一个简单的默认构造函数,它必须是默认的,所有成员构造函数都有

You are bound to use default constructor for var1 within the definition of the constructor. 您必须在构造函数的定义中使用var1默认构造函数。 Ie var1(100) is "no bueno". var1(100)是“no bueno”。 ( Side note: you could use a member ctor but only in a default form eg var1() , which is the same as omitting it ). 旁注:您可以使用成员ctor,但只能使用默认格式,例如var1() ,这与省略它相同 )。

Please note that there is a major difference in using aggregate initialization in declaration of the object, vs setting it as a default. 请注意,在声明对象时使用聚合初始化与将其设置为默认值相比存在重大差异。 It takes some work to set those variables, and a default creation of a POD "can't have none". 设置这些变量需要一些工作,POD的默认创建“不能没有”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM