help with ##

T

Tom Groszko

I need some help with the ## macro statement.
I am writing a macro that will be used multiple times. It will define
variables that will each have to have a different name.
When I try to compile this sample code I get the message that xxxx is
defined more than once. Reading about ## seems like this will solve the
problem but it is not clear to me how to use it.

Can anybody help?
A solution other than using macros is also welcome.

Thanks
Tom G.
class SampleClass

{ public:

SampleClass (const char * abc);

};

#define SampleMacro(somevalue) SampleClass xxxx(somevalue);

void someroutine (void)

{

SampleMacro("somevalue1")

SampleMacro("somevalue2")

}
 
A

Antonio

Tom Groszko said:
Could you please explain your answer, I don't know what to do with it.
Your macro is:
#define SampleMacro(somevalue) SampleClass xxxx(somevalue);

when You write your macro in code:

SampleMacro("pippo");

SampleClass xxx("pippo"); is the code

In next time you write SampleMacro("pluto") and code is SampleClass
xxx("pluto")

and you have error that 'xxx' is defined more than once..

My macro have two parameters, 1st for name of object, and 2nd for value of
constructor ...

Or you define your macro how:
#define SampleMacro(somewhere) SampleClass #somewhere(##somewhere)

but you have an error if somewhere is a ascii-string

Antonio

p.s. sorry for english :(
 
T

tom_usenet

I need some help with the ## macro statement.
I am writing a macro that will be used multiple times. It will define
variables that will each have to have a different name.
When I try to compile this sample code I get the message that xxxx is
defined more than once. Reading about ## seems like this will solve the
problem but it is not clear to me how to use it.

Can anybody help?
A solution other than using macros is also welcome.

Thanks
Tom G.
class SampleClass

{ public:

SampleClass (const char * abc);

};

#define SampleMacro(somevalue) SampleClass xxxx(somevalue);

void someroutine (void)

{

SampleMacro("somevalue1")

SampleMacro("somevalue2")

}

Based on what I think you're trying to do, how about this:

#define STRING_JOIN(x, y) STRING_JOIN_I(x,y)
#define STRING_JOIN_I(x, y) x##y

class SampleClass
{
public:

SampleClass (const char * abc){}
};

#define SampleMacro(somevalue) SampleClass STRING_JOIN(xxxx,
__LINE__)(somevalue);

int main()
{
SampleMacro("somevalue1")
SampleMacro("somevalue2")
}

(remove extra newlines from macros!)

Tom
 
R

Rob Williscroft

tom_usenet wrote in
Based on what I think you're trying to do, how about this:

#define STRING_JOIN(x, y) STRING_JOIN_I(x,y)
#define STRING_JOIN_I(x, y) x##y

Not really string join TOKEN_JOIN perhaps.
class SampleClass
{
public:

SampleClass (const char * abc){}
};

#define SampleMacro(somevalue) SampleClass STRING_JOIN(xxxx,
__LINE__)(somevalue);

int main()
{
SampleMacro("somevalue1")
SampleMacro("somevalue2")

at line N this expands too: SampleClass "somevalue2"N("somevalue2");

I avoided answering this myself as I always get it wrong.

#define STRINGIZE( X ) STRINGIZE_1( X )
#define STRINGIZE_1( X ) #X

#define TOKEN_PASTE( A, B ) TOKEN_PASTE_1( A, B )
#define TOKEN_PASTE_1( A, B ) A ## B

#define SampleMacro( Name ) \
SampleClass TOKEN_PASTE( Name, __LINE__ ) \
( STRINGIZE( Name ) )

Fingers crossed at line N SampleMacro( ident ); should expand to
SampleClass identN ( "ident" );
(remove extra newlines from macros!)


Rob.
 
T

tom_usenet

tom_usenet wrote in

Not really string join TOKEN_JOIN perhaps.

Right, I think CAT is used a lot too.
at line N this expands too: SampleClass "somevalue2"N("somevalue2");

It expands to:

SampleClass xxxxN("somevalue2");

I borrowed the use of the string xxxx from the OPs code.
I avoided answering this myself as I always get it wrong.

#define STRINGIZE( X ) STRINGIZE_1( X )
#define STRINGIZE_1( X ) #X

#define TOKEN_PASTE( A, B ) TOKEN_PASTE_1( A, B )
#define TOKEN_PASTE_1( A, B ) A ## B

#define SampleMacro( Name ) \
SampleClass TOKEN_PASTE( Name, __LINE__ ) \
( STRINGIZE( Name ) )

Why do you need to stringize the name paramenter? In the OPs code it
was already a string.
Fingers crossed at line N SampleMacro( ident ); should expand to
SampleClass identN ( "ident" );

Right, but the OP only needed unique names, and was passing a string
already. He was doing:
SampleMacro("ident")
not
SampleMacro(ident)

I'm not quite sure what he was trying to do, since his identifiers
were not informative!

Tom
 
R

Rob Williscroft

tom_usenet wrote in
Right, I think CAT is used a lot too.


It expands to:

SampleClass xxxxN("somevalue2");

I borrowed the use of the string xxxx from the OPs code.

You're right I missread your example.

Rob.
 

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

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top