Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

C++11 enum class, C++/CLI and forward enum declarations

$
0
0

So one of the new features in C++11  is "enum class" which is great and there is the ability to forward declare enums, also great.

So I ran into a problem after trying to build a C++/CLI wrapper of an unmanaged C++ library that had just been upgraded to use the latest C++11 features, or at least the supported ones. I believe this problem manifests on both VS 2012 and VS 2013.

Imagine that you have a C++ header that looks like this

enum class Foo
{
    Bar
};
enum class Foo;
__declspec(dllimport) void somethingFooey( Foo );

Yes I know it looks odd having an enum definition followed by a forward declaration to the same thing but remember this is a cut down example from a large library where headers include each other etc... and we end up with the above.

So include the above in a file compiled with /CLR and you get the following warning

warning C4272: 'somethingFooey' : is marked __declspec(dllimport); must specify native calling convention when importing a function.

So presumably the compiler thinks that for some reason the function must be _clrcall. However what is odd to me is that if you reverse enum definition and forward declaration the warning goes away. It also goes away if you comment out either the enum definition or the forward declation.

So my question is why the warning only in the one case? Shouldn't the compiler realize that if a function is marked __declspec(dllimport) it can't be _clrcall ( I think this is the case anyway )

I'm assuming from the warning that the answer is to mark the function

__declspec(dllimport) void _cdecl somethingFooey( Foo);

which presumably tells the compiler that this really is an unmanaged function. However this implies I have to get the underlying library to accept decorating some non obvious sub set of functions with _cdecl which may be impossible. I can probably work around this by always including the forward declaration of the enum first on my C++/CLI file but that seems very brittle relying as it does on "magic" orderings of header file includes.

So I'm hoping someone out there with a deeper knowledge of how the C++/CLI compiler does it stuff can suggest a better answer.

David 







Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>