Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > replacement for macro with # and ##

Reply
Thread Tools

replacement for macro with # and ##

 
 
jjleto
Guest
Posts: n/a
 
      12-01-2004
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-01-2004
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 ?


It depends on what you mean by "this". Preprocessor macros are a way to
substitute one string in the source code with another while performing
some operations like converting to literals or merging tokens. It is all
done before the compiler gets to it. Templates are a way to make compiler
generate code when needed based on arguments provided.

What exactly are you trying to accomplish?

V
 
Reply With Quote
 
 
 
 
jjleto
Guest
Posts: n/a
 
      12-01-2004
Victor Bazarov a écrit :
> 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 ?

>
>
> It depends on what you mean by "this". Preprocessor macros are a way to
> substitute one string in the source code with another while performing
> some operations like converting to literals or merging tokens. It is all
> done before the compiler gets to it. Templates are a way to make compiler
> generate code when needed based on arguments provided.


I understand that. But after preprocessing and compiling, we end with
code generated for the macros/templates (although completely
differentely I guess)

> What exactly are you trying to accomplish?
>
> V


I just try to get rid of the macros.
I was thinking of something like

template<char *name>
char *get()
{
/* ... */
return name;
}

and a call like

get< "foo" >();

But this does not compile.


Thanks.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-01-2004
jjleto wrote:
> Victor Bazarov a écrit :
>
>> 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 ?

>>
>>
>>
>> It depends on what you mean by "this". Preprocessor macros are a way to
>> substitute one string in the source code with another while performing
>> some operations like converting to literals or merging tokens. It is all
>> done before the compiler gets to it. Templates are a way to make
>> compiler
>> generate code when needed based on arguments provided.

>
>
> I understand that. But after preprocessing and compiling, we end with
> code generated for the macros/templates (although completely
> differentely I guess)
>
>> What exactly are you trying to accomplish?
>>
>> V

>
>
> I just try to get rid of the macros.


Why?

> I was thinking of something like
>
> template<char *name>
> char *get()
> {
> /* ... */
> return name;
> }
>
> and a call like
>
> get< "foo" >();
>
> But this does not compile.


And it won't. String literals have internal linkage, therefore they
cannot be used as template arguments. But that's not the actual issue
here, is it?

Again, what are you trying to accomplish? Why not, for example, have
the _function_ argument instead of the template argument:

char* get(const char* whattoget) {
if (isname(whattoget))
return name;
else if (issomethingelse(whattoget))
return somethingelse;
else
return NULL;
}

Yes, that's run-time behaviour, not compile-time.

"Getting rid of macros" should not be the goal to pursue. Macros are just
as part of the C++ language as templates, and they are not on their way
out, and won't be any time soon. If they help you organize the code and
make it easier to write and understand, why get rid of them?

Victor
 
Reply With Quote
 
maninalift@another.com
Guest
Posts: n/a
 
      12-03-2004

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 nameublic 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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro expansion vs replacement Pavel C++ 2 06-23-2012 08:22 PM
the replacement rules of the macro? XHengDF@gmail.com C++ 3 03-16-2006 02:21 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments