aCuria
aCuria

Reputation: 7215

Is it possible to share an "enum class" between C++ and C#?

I refer to a previous question: Is it possible to share an enum declaration between C# and unmanaged C++?

I would like to do something similar, but for an enum class instead of an enum:

C++ code:

enum class MyEnum { ONE, TWO, THREE, ... }

C# code:

public enum {ONE, TWO, THREE, ...}

Is there some preprocessor magic that can be done to achieve this? I cant seem to remove the "class" keyword in the C# part of the code.

eg: in MyEnum.cs

#if __LINE__        // if C++
#define public      // C++ code: removes public keyword for C++
#else
#define class       // does not work: unable to remove the class keyword for C#
#endif



enum class MyEnum { ONE, TWO, THREE, ... }


#if __LINE__
#undef public // ok: remove public = nothing definition in C++
#else
#undef class // does not work: #undef must be at the top of the file in C#
#endif

Upvotes: 0

Views: 932

Answers (1)

Soonts
Soonts

Reputation: 21956

C# preprocessor is way more limited compared to the C++ preprocessor. You can leave C# syntax in the source file, and use C++ preprocessor trickery to make it valid C++ code. Like this:

// enum.cs
public enum MyEnum : int { ONE, TWO, THREE };

And to consume in C++:

// enum-cpp.h
#define public
#define enum enum struct

#include "enum.cs"

#undef enum
#undef public

Upvotes: 3

Related Questions