473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

replacement for macro with # and ##

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.
Jul 22 '05 #1
4 1829
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
Jul 22 '05 #2
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.
Jul 22 '05 #3
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
Jul 22 '05 #4

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;

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
3
by: Wolfgang Keller | last post by:
Hello, this is a potentially veeery dumb question, but: - If an application supports VBA as a macro language, - and if you can execute Python code from within a VBA script (how?) - and if the...
20
by: Paul D. Boyle | last post by:
Hi all, There was a recent thread in this group which talked about the shortcomings of fgets(). I decided to try my hand at writing a replacement for fgets() using fgetc() and realloc() to read...
7
by: Fred Zwarts | last post by:
Consider the following definition: typedef struct { int a; int b; } s; Now I have a function void f (int i) { ... }
3
by: XHengDF | last post by:
recent days. i am confuse on the macros in boost. could anyone tell me the replacement rules of the macros in c++, or somelinks about the macros in boost thanks
15
by: Urs Thuermann | last post by:
I want to write a macro that produces debug output and has a variable number of arguments, so that I can use like this: int i; char *s; DBG("simple message\n"); DBG("message with an int...
32
by: chris.fairles | last post by:
Just want an opinion. I have an algorithm that needs to run as fast as possible, in fact. Its already too slow. I've done as much algorithmic changes as I can to reduce the amount of code, so now...
0
by: CodeHulk | last post by:
This is an office automation project. I am currently working on a document control project tracking powerpoint, word, and excel documents. The documents are required to have a specific style of...
11
by: sam_cit | last post by:
Hi Everyone, I have the following question regarding string replacement pre-processor statement, #define status "23" #define status "44" int main() {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.