Am i really right that C standards guarantees that _ _ func _ _ value is always the name of the enclosing function, while in C++ (i mean C++11, of course) it can be any implementation-defined string (for example, if we have function foo without parameters, we can get something like "Some string fdgdg asdfs fsdf sd")?
ISO/IEC 9899:2011
6.4.2.2 Predefined identifiers
Semantics
1 The identifier _ _ func _ _ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration static const char _ func _ [] = "function-name"; appeared, where function-name is the name of the lexically-enclosing function.
ISO/IEC 14882:2011
8.4.1 In general [dcl.fct.def.general]
8 The function-local predefined variable _ _ func _ _ is defined as if a definition of the form static const char _ _ func _ _ [] = "function-name "; had been provided, where function-name is an implementation-defined string. It is unspecified whether such a variable has an address distinct from that of any other object in the program.
And what's the reason for this? Return something like "Unknown" if we can't receive current function name?
What's the "name" of a function in C++?
An "implementation-defined string" gives the compiler some flexibility in how it stuffs all this information into the string. And it sends you, the programmer, a message that you shouldn't try to perform string comparison or parsing, or in fact do anything except log the value, if you want portability.
(You can't even compare these values to each other, because there's no guarantee they're unique. It seems possible that all overloads could yield the same string. So using it for compiling profiler statistics is ill-advised.)
I suspect this is to permit the C++ compiler to use the mangled name in __func__
.
Because C++ permits the overloading of functions based on the type of their parameters, and because functions can be placed in non-global scope (both of which are not allowed in Standard C), the simple name of the lexically-enclosing function is not sufficient to identify it.
For example, consider:
class A {
public:
A() {
std::cout << __func__ << std::endl;
}
void funca() {
std::cout << __func__ << std::endl;
}
};
int main() {
A a;
a.funca();
}
What should it print?
GCC will produce a program that prints "A" and "funca", but the standard permits it to print "A::A" and "a::funca," which the C standard would not allow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With