C ++ Struct completes POD check

I am writing code for the microcontroller and must be sure that my structure is POD. I thought I could use "member initializer lists" to initialize members, but this does not pass the "is_pod" test. In the example below, A is POD, B and C are not. Adding "D () = default;" to D seems to be doing a POD. But by doing this, I can no longer have "member initializer lists"? Is there a way for the structure to be POD and have a "list of member initializers"?

#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 ===
Thanks for the answers! Thus, there seems to be no way to initialize member variables in a structure definition. The following works, but not as elegant as initialization in the structure itself.

typedef struct A_ {
int var1;
} A;

A a = {
    .var1 = 100
};
+4
1

POD , :

, , , , , , .

. , " " .

, :

  • (.. )
  • ,

var1 . var1(100) - "no bueno". ( : ctor, , var1(), ).

, , . , POD " ".

0

Source: https://habr.com/ru/post/1687847/


All Articles