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

坚强是说给别人听的谎言 提交于 2019-12-04 02:50:38

问题


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?


回答1:


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




回答2:


Compile the program using C++11 standards as __func__ is C++11 feature.

So, compile it like:

g++ -std=c++11 foo.cpp -o foo


来源:https://stackoverflow.com/questions/15126387/func-c11-functions-local-predefined-variable-wont-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!