0

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
aCuria
  • 6,935
  • 14
  • 53
  • 89
  • Here is [respective documentation](https://learn.microsoft.com/en-us/cpp/dotnet/how-to-define-and-consume-enums-in-cpp-cli?view=vs-2019#how-to-convert-between-managed-and-standard-enumerations). 10 seconds of googling. Basically looks like you have got this for free. – Marek R Jul 01 '19 at 11:00
  • @MarekR That article you’ve found is for C++/CLI. That’s a strange language, created to mix native code with managed code. While still supported, the language never got much popularity, I don’t think many people are using it nowadays. – Soonts Jul 01 '19 at 16:43
  • @Soonsts Yes and topic is share data between regular `C++` and `C#` so why you are surprised? – Marek R Jul 02 '19 at 07:02
  • @MarekR I’m not a big fan of Microsoft’s proprietary C++ based languages (managed C++, C++/CLI, C++/CX). They complicate builds. They inflate binary size with .NET metadata. They only build on Windows and for Windows, while both C++ and modern .NET are cross-platform. – Soonts Jul 02 '19 at 14:57

1 Answers1

3

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
Community
  • 1
  • 1
Soonts
  • 20,079
  • 9
  • 57
  • 130
  • This code will not compile in both C# and C++, the line #define enum enum struct is not valid, "CS1025 Single-line comment or end-of-line expected". I did try this approach before, but #define only accepts 2 tokens, not one – aCuria Jul 02 '19 at 05:23
  • @aCuria It compiles in C++ on my machine, VC++ 2017. But indeed, not in C#. See the update. – Soonts Jul 02 '19 at 11:36
  • `#define public` and you screwed. Include this header and most of the classes containing `public:` will not compile. – Marek R Jul 02 '19 at 15:01
  • @MarekR It’s OK, I `#undef` them both. – Soonts Jul 02 '19 at 15:03
  • @MarekR Sure, there’re “proper” solutions. Can use these Microsoft’s managed C++ languages, any of them. Can use external code generators to generate both C++ and C# from something else: T4 template on Windows, Python/Ruby, or ProtoBuff also has enumerations. However, all these tools come with non-trivial overhead: weird languages, complicated builds especially if you’re doing CI and building on a server. C preprocessor is more accessible, it just works, and for simple cases like this one does OK job, IMO. – Soonts Jul 02 '19 at 15:09