Search code examples
c++visual-c++c++11visual-studio-2012standards-compliance

__func__ C++11 function's local predefined variable, won't compile


The __func__ C++11 local predefined variable of a function does not compile in Visual Studio 2012 Professional (with Update 1 installed) with the default built-in Visual Studio 2012 (v110) compiler or the November 2012 CTP (v120_CTP_Nov2012) compiler. However, the editor does not complain with any red squiggly underline under __func__. __func__ is supposed to give the name of its containing function, in this case foo, but this neither compiles nor make the editor complain:

#include <iostream>
using namespace std;

void foo()
{
    cout << __func__ << endl;
    return;
}

int main()
{
    foo();
    return 0;
}

It gives the compiler error:

error C2065: '__func__' : undeclared identifier

Am I missing something in my code or will this work in a future update?


Solution

  • MSVC's C99 support is quite poor in general; your best bet might be to use the MSVC-specific __FUNCTION__ macro. See this question for details: Cross-platform defining #define for macros __FUNCTION__ and __func__

    Update (2015-06-22): Visual Studio 2015 supports __func__, see the blog post