Forward an enumeration declaration in C ++ defined in C

I was looking for forward declaration information and did not see any way to make my situation work. So here it is:

1) There is a C-header file, an export interface, so to speak, for large multi-component software containing a typedef enumeration

"export.h":

// This is in "C"! typedef enum _VM_TYPE {...., ...., ...,} VM_TYPE; 

2) Part of the code in C ++ uses this export.

"cpp_code.cpp":

 // This is in C++ #include "export.h" #include "cpp_header.hpp" { .... using VM_TYPE values to do stuffs....} 

"cpp_header.hpp":

 // Need to somehow forward declear VM_TYPE here but how? Struct VM_INFO { .... VM_TYPE VType; //I need to add this enum to the struct .... }; 

So obviously, the problem is in cpp_head.hpp, because it does not know about this listing.

I tried adding to cpp_header.hpp

 typedef enum _VM_TYPE VM_TYPE; 

and it really will work. So why does this work? Because it has C-style syntax ?! In any case, I was told not to do this ("this is C ++, not C here") by the top "control".

Is there any other way to make this work at all based on how things are currently connected? They do not want to modify / add include files; "enum class" is only C ++, right? Adding only "enum VM_TYPE" to cpp_header.hpp will receive an override error.

Any idea? Thanks.

+8
c ++ c enums declaration forward
source share
2 answers

In the specific situation described in your question, you do not need to forward the ad at all. All the files that you #include will essentially be copied into one translation unit before the actual compilation starts, and since you #include "export.h" in front of you #include "cpp_header.hpp" , this will just work, because that by the time the compiler sees the definition of struct VM_INFO , it already saw the definition of enum _VM_TYPE , so you have no problem. There is no difference between including "export.h" in "cpp_header.hpp" and including them in "cpp_code.cpp" in that order, since you end up with the same code after preprocessing. Thus, all you have to do is make sure that you turn it on in the correct order.

If you ever wanted to #include "cpp_header.hpp" without including "export.h" in the translation block, where you need to access the members of struct VM_INFO (so leaving it as an incomplete type is not an option), then "export.h" just poorly designed and you need to work out a definition of what you may need separately in a new heading. If, as follows from the comments, you absolutely cannot do this and must have a suboptimal design, then your next best alternative will consist of two versions of "cpp_header.hpp" that simply repeat the definition of enum _VM_TYPE , and one that doesn’t. You would #include first version in any translation unit, where you also do not #include "export.h" , and #include - the second version in any translation unit where you do. Obviously, any duplication of this type of code will cause problems in the future.

In addition, names starting with an underscore and an uppercase letter are always reserved in C, so you really shouldn't use them. If, in a future version of C, it ever decides to use _VM_TYPE , then you will be stuck with either an outdated version of C or breaking this code.

+1
source share

An enumeration cannot be a redirect because the compiler must know the size of the enumeration. The enumerator is based on a specific compiler, but usually it is int. Can you just translate enum as int?

"I could be and often make mistakes"

0
source share

All Articles