Why would __FUNCTION__ be undefined?

风格不统一 提交于 2019-12-02 05:50:18

Just use

L(__FUNCTION__    L" : A diagnostic message");

When adjacent string literals get combined, the result will be a wide string if any of the components were.

There's nothing immediately wrong with using L as the name of a function... it's rather meaningless however. Good variable and function identifiers should be descriptive in order to help the reader understand the code. But the compiler doesn't care.


Since your L function wraps vsprintf, you may also use:

L(L"%hs : A diagnostic message", __func__);

since __func__ is standardized as a narrow string, the %hs format specifier is appropriate.


The rule is found in 2.14.5p13:

In translation phase 6 (2.2), adjacent string literals are concatenated. If both string literals have the same encoding-prefix, the resulting concatenated string literal has that encoding-prefix. If one string literal has no encoding-prefix, it is treated as a string literal of the same encoding-prefix as the other operand. If a UTF-8 string literal token is adjacent to a wide string literal token, the program is ill-formed. Any other concatenations are conditionally-supported with implementation-defined behavior.

Rob K

You list the error as this:

identifier "L__FUNCTION__" is undefined.

Note it's saying "L__FUNCTION__" is not defined, not "__FUNCTION__".

Don't use __FUNCTIONW__ in your code. MS didn't document that in the page you linked, they documented __FUNCTION__. And you don't need to widen __FUNCTION__.

ETA: I also note that you're not assigning that string to anything or printing it in anyway in f().

I think the definition of __FUNCTIONW__ is incorrect. (I know you did not write it.)

From: http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html

These identifiers are not preprocessor macros. In GCC 3.3 and earlier, in C only, __FUNCTION__ and __PRETTY_FUNCTION__ were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like __func__. In C++, __FUNCTION__ and __PRETTY_FUNCTION__ have always been variables.

At least in current GCC then you cannot prepend L to __FUNCTION__, because it is like trying to prepend L to a variable. There probably was a version of VC++ (like there was of GCC) where this would have worked, but you are not using that version.

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