Can anyone tell when g++ replaces the __FUNCTION__
'macro' with the string containing the function name? It seems it can replace it not until it has check the syntactical correctness of the source code, i.e. the following will not work
#include <whatsneeded>
#define DBG_WHEREAMI __FUNCTION__ __FILE__ __LINE__
int main(int argc, char* argv)
{
printf(DBG_WHEREAMI "\n"); //*
}
since after preprocessing using
g++ -E test.cc
the source looks like
[...]
int main(int argc, char* argv)
{
printf(__FUNCTION__ "test.cc" "6" "\n"); //*
}
and now the compiler rightly throws up because the *ed line is incorrect.
Is there any way to force that replacement with a string to an earlier step so that the line is correct?
Is __FUNCTION__
really replaced with a string after all? Or is it a variable in the compiled code?
Yeah, I know that's not really the same.
You are using
__FUNCTION__
like a preprocessor macro, but it's a variable (please read http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html).Try
printf("%s", __FUNCTION__)
just for testing and it will print the function name.Note that if you create a class, you can build a message from any number of types as you'd like which means you have a similar effect to the << operator or the format in a printf(3C). Something like this:
Note that you could declare the << operators instead of the (). In that case the resulting usage would be something like this:
Depends which you prefer to use. I kind of like () because it protects your expressions automatically. i.e. if you want to output "count << 3" then you'd have to write:
No.
__FUNCTION__
(and its standardized counterpart,__func__
) are compiler constructs.__FILE__
and__LINE__
on the other hand, are preprocessor constructs. There is no way to make__FUNCTION__
a preprocessor construct because the preprocessor has no knowledge of the C++ language. When a source file is being preprocessed, the preprocessor has absolutely no idea about which function it is looking at because it doesn't even have a concept of functions.On the other hand, the preprocessor does know which file it is working on, and it also knows which line of the file it is looking at, so it is able to handle
__FILE__
and__LINE__
.This is why
__func__
is defined as being equivalent to a static local variable (i.e. a compiler construct); only the compiler can provide this functionality.Is this what you want?
Note: Since you marked this as C++ you should probably be using the iostreams to make sure it's type safe.
In C/C++, the preprocessor will turn
"my " "name " "is " "Bob"
into the string literal"my name is Bob"
; since__FILE__
and__LINE__
are preprocessor instructions,"We are on line " __LINE__
will pass "We are on line 27" to the compiler.__FUNCTION__
is normally a synonym for__func__
.__func__
can be thought of as a pseudo-function that returns the name of the function in which it is called. This can only be done by the compiler and not by the preprocessor. Because__func__
is not evaluated by the preprocessor, you do not get automatic concatenation. So if you are usingprintf
it must be done byprintf("the function name is %s", __func__);