为什么__FUNCTION__是未定义的? [英] Why would __FUNCTION__ be undefined?

查看:182
本文介绍了为什么__FUNCTION__是未定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++库,它通过crtdefs.h使用预定义的宏__FUNCTION__.在此处中记录了该宏.这是我的用法:

I have a C++ library that uses the predefined macro __FUNCTION__, by way of crtdefs.h. The macro is documented here. Here is my usage:

my.cpp

#include <crtdefs.h>
...
void f()
{
    L(__FUNCTIONW__ L" : A diagnostic message");
}

static void L(const wchar_t* format, ...)
{
    const size_t BUFFERLENGTH = 1024;
    wchar_t buf[BUFFERLENGTH] = { 0 };
    va_list args;
    va_start(args, format);
    int count = _vsnwprintf_s(buf, BUFFERLENGTH, _TRUNCATE, format, args);
    va_end(args);
    if (count != 0)
    {
        OutputDebugString(buf);
    }
}

crtdefs.h

#define __FUNCTIONW__ _STR2WSTR(__FUNCTION__) 

该库(如果需要的话,编译为静态库)由同一解决方案中的另一个项目使用C#编写的WPF应用程序使用.

The library (which is compiled as a static library, if that matters) is consumed by another project in the same solution, a WPF app written in C#.

编译lib时,出现此错误:

When I compile the lib, I get this error:

标识符"L__FUNCTION__"未定义.

identifier "L__FUNCTION__" is undefined.

根据文档,如果将/P或/EP传递给编译器,则宏不会展开.我已经证实它们不是.还有其他条件无法使用此宏吗?

According to the docs, the macro isn't expanded if /P or /EP are passed to the compiler. I have verified that they are not. Are there other conditions where this macro is unavailable?

推荐答案

只需使用

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.

使用L作为函数名称并没有立即出现问题,但是这毫无意义.好的变量和函数标识符应具有描述性,以帮助读者理解代码.但是编译器不在乎.

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.

由于您的L函数包装了vsprintf,因此您也可以使用:

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

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

由于__func__被标准化为窄字符串,因此%hs格式说明符是合适的.

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

该规则位于2.14.5p13中:

The rule is found in 2.14.5p13:

在翻译阶段6(2.2)中,将相邻的字符串文字串联在一起.如果两个字符串文字都具有相同的 encoding-prefix ,则最终的串联字符串文字具有该 encoding-prefix . 如果一个字符串文字没有 encoding-prefix ,则将其视为与另一个操作数相同的 encoding-prefix 的字符串文字.如果UTF-8字符串文字标记与宽字符串文字标记相邻,则程序格式错误.任何其他串联都由实现定义的行为有条件地支持.

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.

这篇关于为什么__FUNCTION__是未定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆