Monday, June 20, 2011


C++ 2011 and __func__



So what happens to __func__ in different scopes? How about GCC's extension for pretty function names?

#include <iostream>
using namespace std;

namespace
{
void f()
{
auto func = []()->const char *{ return(__func__); };
auto func2 = []()->const char *{ return(__PRETTY_FUNCTION__); }; //GCC extension

cout << func() << '\n'
<< func2() << '\n'
<< __func__ << '\n'
<< __PRETTY_FUNCTION__ << endl;
}
}

int main()
{
f();
cout << __func__ << '\n'
<< __PRETTY_FUNCTION__ << endl;

return(0);
}



Output:

/tmp> g++-4.5 -Wall -o test test.cpp -std=c++0x
/tmp> ./test
operator()
<unnamed>::f()::<lambda()>
f
void<unnamed>::f()
main
int main()
/tmp>

/tmp> g++-4.6 -Wall -o test test.cpp -std=c++0x
/tmp> ./test
operator()
{anonymous}::f()::<lambda()>
f
void {anonymous}::f()
main
int main()
/tmp>

1 comment:

henke37 said...

You got me there, I have no idea what the top two tests are supposed to mean.

And here I thought that I knew how c++ syntax looks like.