__FILE__ marco

P

Plissken.s

Hi,

If I define a template wich uses the __FILE__ marco in a .h file
and I have class which creates/uses that template (in a .cpp file).
What will the __FILE__ marco expand to? the .h which defines the
template? or the .cpp file which uses the template?

Thank you.
 
A

Axter

Hi,

If I define a template wich uses the __FILE__ marco in a .h file
and I have class which creates/uses that template (in a .cpp file).
What will the __FILE__ marco expand to? the .h which defines the
template? or the .cpp file which uses the template?

It will expand to the .h file, where the __FILE__ resides.
 
V

Victor Bazarov

If I define a template wich uses the __FILE__ marco in a .h file
and I have class which creates/uses that template (in a .cpp file).
What will the __FILE__ marco expand to? the .h which defines the
template? or the .cpp file which uses the template?

You should try and see. AFAICT, some compiler can choose to expand it
into the name of the translation unit that starts the chain of include
directives, and some can expand it into the actual file (considering the
included files as well). The Standard (or the way I read it) does not
seem to specify it explicitly.

Oh, BTW, what __FILE__ expands into can be changed by '#line' directive.

V
 
I

Ian Collins

Hi,

If I define a template wich uses the __FILE__ marco in a .h file
and I have class which creates/uses that template (in a .cpp file).
What will the __FILE__ marco expand to? the .h which defines the
template? or the .cpp file which uses the template?
__FILE__ and friends are expanded by the preprocessor, before the
compiler gets a look in. So it will expand to the current file name,
which will be the header in this case.
 
R

Rat Monkey Hybrid

It depends on how the macro is used. If it is used in another macro, it
will expand to where that macro is used.

For instance:

#define PRINT_TRACE \
printf("%s:%i\n", __FILE__, __LINE__);
 
A

Amadeus W. M.

Hi,

If I define a template wich uses the __FILE__ marco in a .h file
and I have class which creates/uses that template (in a .cpp file).
What will the __FILE__ marco expand to? the .h which defines the
template? or the .cpp file which uses the template?

Thank you.

Taking a wild guess here, you might want to try this:


#define WhereMacro \
cerr << __FILE__ << ": line: " << __LINE__ << ": " << __FUNCTION__ <<endl


in some header file, then call

WhereMacro;

anywhere in your code, and it all will expand to what you expect.

This is one instance where you have to use a #define, rather than an
inline function.
 
M

Maxim Yegorushkin

D

Dave Townsend

Hi,

If I define a template wich uses the __FILE__ marco in a .h file
and I have class which creates/uses that template (in a .cpp file).
What will the __FILE__ marco expand to? the .h which defines the
template? or the .cpp file which uses the template?

Thank you.

One test case is worth a thousand expert opinions, why don't you write a
piece
of code to see what the compiler does, or are you an armchair programmer?

dave
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top