|
|||
|
[g++ 4.4.0, linux, large SW project so I can't change compilers or the
api I'm talking to] I have a macro: #define RegisterFactory(clazz) \ clazz clazz##_instance; \ extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \ Foo::HHRegistrar clazz##_registrar(#clazz) which works fine for literal arguments. But when I give it an argument that was defined on the compile line: g++ -DFACTORYNAME=MyFactory it fails. I understand why... the arg is used in paste and stringify contexts -- but is there some reasonable way to fix this? I want RegisterFactory(FACTORYNAME) to expand to: MyFactory MyFactory_instance; extern "C" Foo::HHFactory *getMyFactory() { return &MyFactory_instance; } \ Foo::HHRegistrar MyFactory_registrar("MyFactory") The original macro is used in many places so I need something that cleanly replaces it. I tried #define FACTORYNAME MyFactory, but no difference(not that I really expected one). |
|
|
||||
|
||||
|
|
|
|||
|
gry <georgeryoung@gmail.com> writes:
> #define RegisterFactory(clazz) \ > clazz clazz##_instance; \ > extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \ > Foo::HHRegistrar clazz##_registrar(#clazz) > which works fine for literal arguments. But when I give it an > argument that was defined on the compile line: > > g++ -DFACTORYNAME=MyFactory > > it fails. Define a new macro that takes FACTORYNAME as an argument and forwards it to your original macro (renamed below): #define RegisterFactoryNoExpand(clazz) \ clazz clazz##_instance; \ extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \ Foo::HHRegistrar clazz##_registrar(#clazz) #define RegisterFactory(clazz) RegisterFactoryNoExpand(clazz) #define FACTORYNAME MyFactory RegisterFactory(FACTORYNAME); Then, the preprocessor will expand FACTORYNAME. http://c-faq.com/ansi/stringize.html |
|
|||
|
On Apr 14, 2:25*am, Kalle Olavi Niemitalo <k...@iki.fi> wrote:
> gry <georgeryo...@gmail.com> writes: > > #define RegisterFactory(clazz) \ > > * * clazz clazz##_instance; \ > > * * extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \ > > * * Foo::HHRegistrar clazz##_registrar(#clazz) > > which works fine for literal arguments. *But when I give it an > > argument that was defined on the compile line: > > > g++ -DFACTORYNAME=MyFactory > > > it fails. > > Define a new macro that takes FACTORYNAME as an argument and > forwards it to your original macro (renamed below): > > #define RegisterFactoryNoExpand(clazz) \ > * * clazz clazz##_instance; \ > * * extern "C" Foo::HHFactory *get##clazz() { return &clazz##_instance; } \ > * * Foo::HHRegistrar clazz##_registrar(#clazz) > #define RegisterFactory(clazz) RegisterFactoryNoExpand(clazz) > > #define FACTORYNAME MyFactory > RegisterFactory(FACTORYNAME); > > Then, the preprocessor will expand FACTORYNAME.http://c-faq.com/ansi/stringize.html That works beautifully. Thanks! |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|