Preprocessor question related to C2121 - '#' : invalid character : possibly the result of a macro ex

G

Gustavo L. Fabro

Greetings.

Is there a way to run the preprocessor twice? Rephrasing that, is there a
way to:

#define SOMETHING #pragma OTHERTHING

and have the preprocessor in somecode.cpp evaluate:

SOMETHING

as

#pragma OTHERTHING

and then evaluate this #pragma directive as it should?

I know the C++ standard under 16.3.4 says:

"The resulting completely macro replaced preprocessing token sequence is not
processed as a preprocessing directive even if it resembles one."

but the C standard (1999) under 6.10.3.4 appears to change this to allow
#pragma directives in macros:

"The resulting completely macro-replaced preprocessing token sequence is not
processed as a preprocessing directive even if it resembles one, but all
pragma unary operator expressions within it are then processed as specified
in 6.10.9 below."

I know C is not C++ and so MSVC shouldn't actually allow this over my C++
code (if I'm interpreting the standard right). But is there a way to do it?
;-)

In case you want to know why I want this: I'm performance tuning my
application and I have a couple of functions and classes I'd like to compile
as native code.

Some of these classes have methods that call .NET Framework functions. I
have to explicity set these particular methods with a #pragma managed so
they will compile, Interop will do its job and it will run.

My code is being compiled under 2 different compilers. I'm using #define and
#ifdef directives to accomplish this. And my other compiler keeps shooting
me warnings over unknows #pragmas (such as #pragma unmanaged, for instance).

Having said that, I'd like to define things like:

#ifdef VISUAL_C //I #define this when using my MSVC compiler
#define SET_CLR_MODE #pragma managed
#define SET_NATIVE_MODE #pragma unmanaged
#else
#define SET_CLR_MODE
#define SET_NATIVE_MODE
#endif

and just SET_CLR_MODE whenever I needed IL and SET_NATIVE_MODE when I wanted
to go back to native.
Is it possible?

Thanx,

Fabro
 
D

Doug Harrison [MVP]

I know the C++ standard under 16.3.4 says:

"The resulting completely macro replaced preprocessing token sequence is not
processed as a preprocessing directive even if it resembles one."

but the C standard (1999) under 6.10.3.4 appears to change this to allow
#pragma directives in macros:

I know C is not C++ and so MSVC shouldn't actually allow this over my C++
code (if I'm interpreting the standard right). But is there a way to do it?

Having said that, I'd like to define things like:

#ifdef VISUAL_C //I #define this when using my MSVC compiler
#define SET_CLR_MODE #pragma managed
#define SET_NATIVE_MODE #pragma unmanaged
#else
#define SET_CLR_MODE
#define SET_NATIVE_MODE
#endif

and just SET_CLR_MODE whenever I needed IL and SET_NATIVE_MODE when I wanted
to go back to native.
Is it possible?

Offhand, I don't know the answer to your question, but if it turns out you
can't directly accomplish it, you could fall back on the method used by the
"pshpackN.h" and "poppack.h" header files. That is, you would define header
files such as:

gomanaged.h:

#ifdef VISUAL_C
#pragma managed
#endif

gounmanaged.h:

#ifdef VISUAL_C
#pragma unmanaged
#endif

Then you would #include these files instead of using the #pragmas directly.
 
G

Gustavo L. Fabro

Offhand, I don't know the answer to your question, but if it turns out you
can't directly accomplish it, you could fall back on the method used by
the
"pshpackN.h" and "poppack.h" header files. That is, you would define
header
files such as:

gomanaged.h:

#ifdef VISUAL_C
#pragma managed
#endif

gounmanaged.h:

#ifdef VISUAL_C
#pragma unmanaged
#endif

Then you would #include these files instead of using the #pragmas
directly.

Hi there!

Indeed that was my first plan. I just though it would be a bit nasty to have
#include directives in the middle of my function implementations.

I decided to google that particular piece of the standard and found several
messages on the subject. Guess it's really impossible to do it in the
#define way I had planned.

But thank you for the reply, anyway! :)

Fabro
 
Top