Preprecessor and white spaces

G

Gernot Bauer

Hi!

I am trying to automate some work by using the preprocessor. Please
consider the following example:


-----------------------------------------------------------------
#define MY_CLASS(ClassType) \
class ##ClassType \
{ \
}

MY_CLASS(MyClassName);

int main()
{
return 0;
}

-----------------------------------------------------------------

The preprocessor output looks like this (I am using VC 8.0):

-----------------------------------------------------------------
classMyClassName { };

int main()
{
return 0;
}
-----------------------------------------------------------------

As you can see, the (intended) white space between the keyword class and
the argument is deleted, thus the generated code is not compileable.

Is there any way to get a white space between class and MyClassName? I
already searched on the web, but did not find anything.

Any help would be highly appreciated!

Kind regards,
Gernot
 
G

Gernot Bauer

^^
This has to be deleted, of course. I don't know why I put it into my
macro :/ Now everything seems to work as expected.

Kind regards,
Gernot
 
G

Greg Comeau

Hi!

I am trying to automate some work by using the preprocessor. Please
consider the following example:


-----------------------------------------------------------------
#define MY_CLASS(ClassType) \
class ##ClassType \
{ \
}

MY_CLASS(MyClassName);

int main()
{
return 0;
}

-----------------------------------------------------------------

The preprocessor output looks like this (I am using VC 8.0):

-----------------------------------------------------------------
classMyClassName { };

int main()
{
return 0;
}
-----------------------------------------------------------------

As you can see, the (intended) white space between the keyword class and
the argument is deleted, thus the generated code is not compileable.

Is there any way to get a white space between class and MyClassName? I
already searched on the web, but did not find anything.

Any help would be highly appreciated!

You could get rid of ##. I'm sure you've only shown enough code to
zero in on the problem you're having, but I think most of us are
curious about what even that accomplishes especially considering the
default of class whatever {};?
 
G

Gernot Bauer

Hello!

Greg said:
You could get rid of ##. I'm sure you've only shown enough code to

As stated in my other post, I already figured that out. I make seldom
use of the preprocessor and somehow only thought about string
concatenation. Sometimes, you don't see the wood for the trees.
zero in on the problem you're having, but I think most of us are
curious about what even that accomplishes especially considering the
default of class whatever {};?

This is, as you guessed, only a minimal example to illustrate my problem.

The real problem I tried to address is the following: My custom
exception classes I use are rather basic and resemble std::exception.
When I create an exception hierarchy, I mainly have to do c&p. This is
annoying, and as it annoys me, I sometimes forget to make necessary
changes to the new class which results in a compiler error that annoys
me even more.
With the macros I wrote, I can automate this to some extent so I only
have to write something like

ABSTRACT_BASE_EXCEPTION(ExceptionBase);
DERIVED_EXCEPTION(ExceptionDerived, ExceptionBase, "My Default String");

in order to create those exception classes for me. I believe this also
helps me when I want to refactory my exception hierarchy (another
boring, but error-prone duty).

By now, I have written and tested the macros. I still have to test their
usefullness and how well they work together with automated documentation
tools like doxygen.

Kind regards,
Gernot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top