can I make #define macros visible in another file?

A

aaragon

Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:

// file.h
#define OBJECTIVE template <class T> \
void operator()(T& chrom_,double* fitness_);

// file main.cpp

OBJECTIVE
{
// definition for objective
}

I've been trying to do this but it doesn't work. It seems that the
macros can only be used in the same file? Any suggestions? Thank you
all.

a^2
 
R

red floyd

aaragon said:
Hi everyone, I have a simple question. I'm trying to make a macro in
one file so I can use it in main.cpp. The idea is that I the user of
my code has simple to type the macro definition to replace a more
complicated code. Something like the following:

// file.h
#define OBJECTIVE template <class T> \
void operator()(T& chrom_,double* fitness_);

Delete the semicolon.

However, in general, this is not a good idea.
// file main.cpp

OBJECTIVE
{
// definition for objective
}

I've been trying to do this but it doesn't work. It seems that the
macros can only be used in the same file? Any suggestions? Thank you
all.


did you #include "file.h"
 
R

red floyd

red said:
Delete the semicolon.

However, in general, this is not a good idea.

Let me clarify my "not a good idea". I'm not talking about killing the
semicolon. I'm saying that this sort of usage is in general not a good
idea.

What problem are you trying to solve?
 
A

aaragon


Well, I'm writing a library and I want it user-friendly for people
who're gonna use it. This library uses a functor that must be written
by the user. Therefore, instead of allowing the user to write
completely the code for the functor, I just wrote the functor and the
declaration of operator in a .h file that will be included by the
user:

// file.h
class Objective
{
public:

template <class T>
void operator()(T& chrom_,double* fitness_);

};

#define OBJECTIVE template <class T> \
void Objective::eek:perator()(T& chrom_,double* fitness_)


Therefore, the user of the library has to type OBJECTIVE in main.cpp
to write the templated syntax for operator(). The way I use the macro
here is just for string replacement so I thought it was a good idea.
Do you have a better suggestion?
 
J

John Harrison

aaragon said:
Well, I'm writing a library and I want it user-friendly for people
who're gonna use it. This library uses a functor that must be written
by the user. Therefore, instead of allowing the user to write
completely the code for the functor, I just wrote the functor and the
declaration of operator in a .h file that will be included by the
user:

// file.h
class Objective
{
public:

template <class T>
void operator()(T& chrom_,double* fitness_);

};

#define OBJECTIVE template <class T> \
void Objective::eek:perator()(T& chrom_,double* fitness_)


Therefore, the user of the library has to type OBJECTIVE in main.cpp
to write the templated syntax for operator(). The way I use the macro
here is just for string replacement so I thought it was a good idea.
Do you have a better suggestion?

Macros can of course be defined in one file and used in another.
Standard header files define several macros.

I suspect you are running into another problem. Templates must be fully
defined in header files. You cannot get you user to write a templated
method in a cpp file and expect it to link with your library code.
You're going to have to find some other solution.

John
 
C

Chris Theis

Apart from the problem with the templates that John already pointed out,
define macros can be the source of hours of tiresome "debugging", where
there is actually no bug. The issue is that the preprocessor might modify
your code in a way that you do not intend and this can lead for example to
confusing error messages that have absolutely nothing to do with the actual
problem that you're facing. I just had an example of old code which fixed
the non compliance of for statements of an old compiler with such a define.
Upgrading to a newer version of the compiler where I used "for" in a #pragma
got me this "for" replaced by the define and this resulted in an absolutely
nonsencial error message, which took me quite some time to figure out.

Thus, I'd consider trading transparency to the library user for some more
coding on their part. But I can also understand that you're trying to make
things as nice as possible. Just keep the FAQ in mind
(http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5) -
"Macros are bad for your health ;-)"

Cheers
Chris
 
A

aaragon

Apart from the problem with the templates that John already pointed out,
define macros can be the source of hours of tiresome "debugging", where
there is actually no bug. The issue is that the preprocessor might modify
your code in a way that you do not intend and this can lead for example to
confusing error messages that have absolutely nothing to do with the actual
problem that you're facing. I just had an example of old code which fixed
the non compliance of for statements of an old compiler with such a define.
Upgrading to a newer version of the compiler where I used "for" in a #pragma
got me this "for" replaced by the define and this resulted in an absolutely
nonsencial error message, which took me quite some time to figure out.

Thus, I'd consider trading transparency to the library user for some more
coding on their part. But I can also understand that you're trying to make
things as nice as possible. Just keep the FAQ in mind
(http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5) -
"Macros are bad for your health ;-)"

Cheers
Chris

I understand your point and now I know that if I run into problems,
the macros I defined are the first place to look at. However, I'm not
using macros as functions but only as pure text replacements. I got my
code working and it seems do the right thing without any problems. I
just want the user to type OBJECTIVE instead of the more complicated
and error-prone

template <class T>
void operator()(T& chrom_,double* fitness_)

Thanks for all your advice.

a^2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top