views:

29

answers:

1

My problem is quite simple. I want the following macro

#define PROXYPASS(name, param) \
void MyClass::sl_name(param _param) { \
    emit name(_param); \
}

to expand PROXYPASS(clientAdded, Client*) to:

void MyClass::sl_clientAdded(Client* _param) { \
    emit clientAdded(_param); \
}

but since it ain't working i.e. it still shows just sl_name instead of sl_clientAdded. So, this is what I'm using:

#define PROXYPASS(name, param) \
void MyClass::sl_ ## name(param _param) { \
    emit ## name(_param); \
}

It works fine, other than the fact that it expands to:

void MyClass::sl_clientAdded(Client* _param) { \
    emitclientAdded(_param); \
}

Everything is fine other than the fact that there is no space between emit and clientAdded i.e. it expands to emitclientAdded instead of emit clientAdded. So how do I go about doing it? Is there a way to add spaces, or must I look for another way. A lot of googling has got my hopes down due to the following statement: Whether it [gcc pre-processor] puts white space between the tokens is undefined.

My other failed attempts include

emit ## ## name(_param);
emit ## /* */ ## name(_param);
emit ## \ ## name(_param);
emit /* */ ## name(_param);
emit ## space ## name(_param); [#define space \ ]

Any help is greatly appreciated.

NOTE: Preprocessor output and macro expansion checked using gcc -E myclass.cpp.

A: 

Just put nothing there, you don't need to concatenate anything:

#define PROXYPASS(name, param) \
void MyClass::sl_ ## name(param _param) { \
    emit name(_param); \
}
jpalecek
`emit name(_param)` expands to only `clientAdded(_param);`. For some strange reason the `emit` keyword is simply gobbled. I do think it is because it is not standard C++, because if I change it to `int name(_param)`, it expands properly with `int clientAdded(_param);`. I think it is because `emit` in itself is a macro.
Rohan Prabhu
Yes, it is exactly so. The `emit` "keyword" expands to nothing, it has no real meaning. Qt's signals behave like, and in fact are functions, and can be called without `emit`.
jpalecek
yes, "emit" is just syntactical sugar.
Frank
thanks a lot. I guess I was doing what was required all along :)
Rohan Prabhu