473,324 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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

Nov 17 '05 #1
2 4863
On Thu, 12 May 2005 17:49:15 -0300, Gustavo L. Fabro wrote:
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."
<snip>
but the C standard (1999) under 6.10.3.4 appears to change this to allow
#pragma directives in macros:
<snip>
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?
<snip>
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.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
> 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
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: qazmlp | last post by:
I hope comp.lang.c will not find the following question as a complete off-topic. I would like to remove ie.comment out the 'cout' statements during compilation(actually preprocessing) time. ...
25
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
18
by: /* frank */ | last post by:
My teacher said that array in C is managed by preprocessor. Preprocesser replace all array occurences (i.e. int a ) with something that I don't understand/remember well. What's exactly happens...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
8
by: claus.tondering | last post by:
I need to write a macro that inserts someStruct m_someStruct; into another struct declaration. The problem is that if the programmer specifies one particluar struct (called alpha), nothing...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
6
by: olivier.grant | last post by:
Hi All, I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE ) and end up with the following preprocessed code : #include NAME.hpp
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.