Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gustavo L. Fabro - 12 May 2005 21:49 GMT
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
Doug Harrison [MVP] - 12 May 2005 22:14 GMT
> 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:
>
[quoted text clipped - 9 lines]
> 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.

Signature

Doug Harrison
Microsoft MVP - Visual C++

Gustavo L. Fabro - 13 May 2005 02:01 GMT
> 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
[quoted text clipped - 17 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.