Will static_assert in POD destroy POD?

I'm just curious ... Say I have a POD structure in C ++. If I put static_assert there, would it ruin the fact that this is a POD?

I know that I can easily put it in another place, I just ask, because I wonder if I should or should not do this ...

In other words (more specific):

 #include <iostream> #include <type_traits> struct A { void* ptr; static_assert(sizeof(void*) == 8, "Pointer should have size 8; platform unsupported"); }; int main() { // Is it guaranteed that this will evaluate to 'true'? std::cout << std::is_pod<A>::value << std::endl; } 
+5
source share
1 answer

In C ++ 11, a type is considered a POD if it is

  • trivial (scalar type, trivially copied class with trivial default constructor or an array of this type / class)
  • standard layout (no virtual functions, no virtual base classes, etc.)

In principle, nothing that could interfere with copying objects, as if they consist only of raw bytes.

static_assert is to check something at compile time and does not change the layout of the object or triviality (or lack thereof)) when building, copying, etc. object. Therefore, adding any number of static statements to a type (struct / class) should not change its POD version.

You can check if the compiler is considering the POD type with std::is_pod<T>::value . This would not change before and after adding static_assert to it.

This is all the standard says about static_assert s. From [dcl.dcl] :

In a static_assert declaration, a constant expression must be a constant expression that can be contextually converted to bool . If the meaning of the expression, when it is converted, is true, the declaration is not valid. Otherwise, the program is poorly formed, and as a result, the diagnostic message (1.4) should contain the text of the string literal, except that the diagnostic message should not contain characters that are not contained in the basic character set of the source (2.3).

+4
source

All Articles