Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__func__ value difference between C and C++

Tags:

c++

c

c++11

c99

c11

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?

like image 341
FrozenHeart Avatar asked Jan 04 '13 20:01

FrozenHeart


3 Answers

What's the "name" of a function in C++?

  • Is it qualified by its namespace?
  • Are overloaded functions named by their parameter types?
  • What about return type, which isn't used for overloading?
  • What happens in function templates?
  • Is calling convention part of the name?
  • Should 'const' and 'volatile' be written before or after the type name?

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.)

like image 50
Ben Voigt Avatar answered Oct 11 '22 22:10

Ben Voigt


I suspect this is to permit the C++ compiler to use the mangled name in __func__.

like image 45
NPE Avatar answered Oct 11 '22 22:10

NPE


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.

like image 25
greyfade Avatar answered Oct 11 '22 21:10

greyfade