jjleto wrote:
I have a C program that uses in some parts macros with # and ## like
in:
#define GET_FUNC_DECL(name) char *get##name();
#define GET_FUNC_IMPL(name) char *get##name() { /* some stuff */;
return #name; }
Is there a way to achieve this without macros in C++ ? With templates
perhaps ?
Thanks.
using macros and ## is the ONLY way to generate identifiers (i.e.
function and type names). Presumabaly the GET_FUNC_DECL macro also
takes some other parameters.
You can use templates but the "name" would have to be an identifier of
it's own (after all, that is closer to what "name" is in the macro).
For instance you could write:
template </*...parameters which define what the function does..*/>
class a_base_class{
rtn_type static get();
rtn_type static do_somthing_else();
};
template</*..same stuff again*/>
a_base_class<parameter>::get(){/*default implementation*/}
//spaecialuizations
then to give a specific name to the function just define
class name:public a_base_class<..parameters..>{};
//which can then be used like:
int main(){
name::get();
name::do_somthing_else();
}
PERHAPS BETTER TO USE TYPEDEF RATHER THAN INHERITANCE (THOUGH
INHERITENCE ALLOWS YOU TO BE MORE EXPLICIT IN SPECIALIZATIONS:
typedef a_base_class<with these parameters> name;