警告C6031返回值在宏扩展中被忽略 [英] warning C6031 return value ignored in macro expansion
问题描述
我正在使用以下代码格式化 HRESULT
消息,并将消息写入文件,仅当 HRESULT
为错误。
I'm using following code to format HRESULT
to message and write the message to file only if HRESULT
is an error.
该代码可以编译并正常运行,只不过我正在关注编译器警告:
The code compiles and works fine, except that I'm getting following compiler warning:
警告C6031返回值被忽略:'wcsrchr'。
Warning C6031 Return value ignored: 'wcsrchr'.
我不想禁用警告,但要解决它,但是我'不知道怎么办?
这是最低的可编译代码:
I don't want to disable warning but to resolve it, but I'm not able to figure out how? Here is a minimum compilable code:
// compile with: /Wall
#include <Windows.h>
#include <cwchar> // std::wcsrchr
#include <comdef.h> // _com_error
#include <iostream> // std::cin
// Show only file name instead of full path wide version
#define __FILENAME__ (std::wcsrchr(TEXT(__FILE__), L'\\') ? std::wcsrchr(TEXT(__FILE__), L'\\') + 1 : TEXT(__FILE__))
// Writes a sprintf-formatted string to the logging file.
#define TRACE(...) DebugLogTrace(__VA_ARGS__)
// Log HRESULTs if failed.
#define LOG_IF_FAILED(file_name, line, hr) if constexpr (FAILED(hr)) \
{ TRACE((TEXT("%s %i %s"), file_name, line, _com_error(hr).ErrorMessage())); }
// Writes a sprintf-formatted string to the logging file.
void DebugLogTrace(PCTSTR format_string, ...) noexcept
{
// implementation not important
}
int main()
{
// generate example failure
LOG_IF_FAILED(__FILENAME__, __LINE__, E_FAIL);
std::cin.get();
return 0;
}
E_FAIL $ c $的示例文件输出c>错误代码:
7:50:11未指定错误
7:50:11 Unspecified error
推荐答案
我能够通过更改以下内容来消除警告:
I was able to get the warning to go away by changing:
#define LOG_IF_FAILED(file_name, line, hr) if constexpr (FAILED(hr)) \
{ TRACE((TEXT("%s %i %s"), file_name, line, _com_error(hr).ErrorMessage())); }
至
#define LOG_IF_FAILED(file_name, line, hr) if constexpr (FAILED(hr)) \
{ TRACE(TEXT("%s %i %s"), file_name, line, _com_error(hr).ErrorMessage()); }
即: {TRACE((...,... ,...,...))); }
到 {TRACE(...,...,...,...); }
但是我不得不承认,我不知道删除多余的括号是否还有其他意外结果。
But I have to admit, that I don't know if there are some other unintended result of removing the extra parenthesis.
这篇关于警告C6031返回值在宏扩展中被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!