Re: replacement for macro with # and ##

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 12/01/04


Date: Wed, 01 Dec 2004 16:18:08 -0500

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



Relevant Pages

  • Re: Combinations/permutations algorithm in C++
    ... > of type long, double or string. ... Let the compiler generate it for you on the fly via templates. ... // handling parameter 1 ...
    (comp.lang.cpp)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... Big-O notation isn't mathematics per se, it's computer science notation ... "length of the string". ... outrun something compiled and optimized by a good C compiler. ... > either general computing culture or culture outside computing. ...
    (comp.programming)
  • Re: [EGN] Hoisting Loop Invariants (Was: Re: [EGN] Numerical Accuracy)
    ... compiler out there somewhere that did as you claim. ... > the programmer has this knowledge, then the programmer should not use ... >> string in a loop, regardless of the blatant inefficiency of doing so. ...
    (comp.programming)
  • Re: How to convert Infix notation to postfix notation
    ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
    (comp.lang.c)
  • Re: PL/I string representations
    ... >> of the language, so it was interesting to me, hopefully it will be to ... I found a workable compiler for Fortran in 1971 (with a bug ... >> The specified length is the minimum, and each time a character ... >> string is assigned to E, the length is stored with it. ...
    (comp.programming)

Loading