10

Is this struct a POD in C++11?

struct B
{
  int a;
  B(int aa) : a(aa) {}
  B() = default;
};

Note that this question is explicit about C++11. I know that this class is not a POD in C++98 nor C++03.

For an explanation of POD in C++11, see trivial vs. standard layout vs. POD

(Inspired by this question: Is there a compile-time func/macro to determine if a C++0x struct is POD? )

Jonas
  • 121,568
  • 97
  • 310
  • 388
Sjoerd
  • 6,837
  • 31
  • 44

1 Answers1

14

Yes, it is a POD according to the new rules.

If you look up paragraph §8.4.2/4 of the new standard, you can see that if a constructor is defaulted on the first declaration, it is not user-provided:

Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them (§12.1 §12.4, §12.8), which might mean defining them as deleted. A special member function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. (...)

You can use the std::is_pod type trait to have the compiler test this for you with static_assert.

static_assert(std::is_pod<B>::value, "B should be a POD");
Community
  • 1
  • 1
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • 1
    I don't trust compilers to get this right yet. In fact, I couldn't even find a compiler that would compile my code without choking on the `= default` part! – Sjoerd Aug 24 '11 at 02:18
  • GCC 4.5 supports that, and that's what's running behind ideone.com. Sometimes I forget that not every other compiler is at the same level of support of C++11 features :( – R. Martinho Fernandes Aug 24 '11 at 02:19
  • Thanks for the quote, it was the missing link for me. A default constructor is not trivial if it is user-provided, and your quote makes it clear that the `B() = default;` does not count as user-provided. – Sjoerd Aug 24 '11 at 02:24
  • 1
    @Sjoerd: don't forget that `= default` must appear on the first declaration (which will be inside the class). It is not trivial if you declare `B();` in the class and then do `B::B() = default;`. – R. Martinho Fernandes Aug 24 '11 at 02:28