Is there a way to detect whether #pragma unmanaged is in effect in C++/CLI?

只愿长相守 提交于 2019-12-08 15:16:49

问题


I have a project that includes some performance sensitive native C++ headers making heavy use of templates. For this project we also wrap the headers and add some glue code to expose the functionality to c# and other .NET languages. We'll call this header "layout.h", and we'll assume it's a third party header that I can't change.

In a mixed mode C++/CLI assembly it is relatively easy to make a mistake and #include from a place in the code where #pragma unmanaged (or #pramga managed(push,off) ) . When that happens the templates generate IL, and I get extra managed/unmanaged transitions when running the code and performance goes down the drain.

My question is whether there is a way I can do a compile-time check just before the #include so that compilation fails if I am accidently #including from the wrong context.

// File1.cpp, compiled in a mixed mode C++/CLI assembly with /clr
    ASSERT_UNMANAGED()
    #include <layout.h>

My naive 1st attempt checked #ifdef _MANAGED, but that is always defined whether I'm in a #pragma unmanaged block of code or not.


回答1:


The pragma directives must be inserted directly in the include file. In this way, everywhere you include the file an unmanaged section is declared.

Sorry that you have to modify your include file.




回答2:


You may write ASSERT_MANAGED or ASSERT_UNMANAGED code that would use construct that is available ONLY while compiling managed or unmanaged. A ref class declaration is an example which is avaiable only when using managed.

This is somewhat a dirty solution, but it would work.




回答3:


Here's a possible solution, making use of the fact that intrinsics are always compiled as native (unmanaged) code:

#include <intrin.h>

#define ASSERT_UNMANAGED() \
int TestFunc(void) { \
    __pragma(warning(push)) \
    __pragma(warning(error:4793)) \
    auto aumt = [] () { return _bextr_u64(65537, 0, 8); }; \
    __pragma(warning(pop)) \
    return int(aumt()); }

#pragma unmanaged   // Comment out this line and the assertion fails!
ASSERT_UNMANAGED()
#pragma managed

EDIT: Of course, if you just want warnings rather than compilation failure, you can remove the 3 __pragma(warning()) lines.



来源:https://stackoverflow.com/questions/7039132/is-there-a-way-to-detect-whether-pragma-unmanaged-is-in-effect-in-c-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!