本文介绍了为什么__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:

根据文档,如果将/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:

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

08-01 21:51