Make the C ++ part compile as C

I want to define a structure in the C ++ source code, which should be POD (therefore, it must be compiled based on C, not C ++)

for example, suppose I have the following code inside a C ++ file:

struct myStruct { int x; int y; } class MyClass { int x; int y; } 

If I compile this code, the struct will be a POD and should be compiled as a POD. Thus, the placement of member variables follows the C standard, which is well defined.

But suppose the user can erroneously change the code to this code:

 struct myStruct { int x; int y; private: int z; } class MyClass { int x; int y; } 

now the structure is not a POD, and the compiler is free of how it puts member variables into memory.

How to make the compiler make sure that the structure is always compiled based on the C standard?

Please note that I cannot put the code inside the * .c code, since I only develop header code that can be included in the * .cpp source code.

+5
source share
1 answer

You cannot force the translator to treat it as "C". But you can add the statement that it is compatible with C code.

 #include <type_traits> struct myStruct { int x; int y; }; static_assert(std::is_pod_v<myStruct>, "Violated POD-ness of myStruct!"); 
+11
source

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


All Articles