473,569 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4897
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
1501
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. The statements like this: cout<<"something\n" ; should be made as
25
9091
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 <iostream> #include <string> using namespace std; #define B(X, Y) Y
205
10471
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
3007
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 with array during preprocessing/compiling stage? Thanks in advance
13
2109
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 defined = 2;
9
3646
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. Could someone point me to the appropriate section, or else confirm the behaviour as a bug? For a particular project, I am using the C preprocessor...
8
2904
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 should be inserted. So, is it possible to write a macro that does this: MACRO(alpha) expands to nothing
32
2750
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 the same file ? If not then how much variation is allowed ? Is it just a bit more or less white space here and there or could could there be larger...
6
3114
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
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.