警告 C6031 return 值在宏扩展中被忽略
warning C6031 return value ignored in macro expansion
我正在使用以下代码将 HRESULT
格式化为消息,并且仅当 HRESULT
出错时才将消息写入文件。
代码编译并运行良好,除了我正在关注 compiler warning:
Warning C6031 Return value ignored: 'wcsrchr'.
我不想禁用警告而是要解决它,但我不知道如何解决?
这是一个最小的可编译代码:
// 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
错误代码的示例文件输出:
7:50:11 Unspecified error
我可以通过更改来让警告消失:
#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(..., ..., ..., ...); }
但我不得不承认,我不知道删除多余的括号是否还有其他意想不到的结果。
我正在使用以下代码将 HRESULT
格式化为消息,并且仅当 HRESULT
出错时才将消息写入文件。
代码编译并运行良好,除了我正在关注 compiler warning:
Warning C6031 Return value ignored: 'wcsrchr'.
我不想禁用警告而是要解决它,但我不知道如何解决? 这是一个最小的可编译代码:
// 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
错误代码的示例文件输出:
7:50:11 Unspecified error
我可以通过更改来让警告消失:
#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(..., ..., ..., ...); }
但我不得不承认,我不知道删除多余的括号是否还有其他意想不到的结果。