警告 C6031 返回值在宏扩展中被忽略
warning C6031 return value ignored in macro expansion
我使用以下代码将HRESULT
格式化为消息,并仅在HRESULT
错误时才将消息写入文件。
代码编译并工作正常,除了我收到以下编译器警告:
警告 C6031 返回值被忽略:"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 未指定的错误
我能够通过更改来获得警告:
#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(..., ..., ..., ...); }
但我不得不承认,我不知道删除多余的括号是否还有其他一些意想不到的结果。
相关文章:
- 是否可以通过C++扩展强制多个python进程共享同一内存
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 扩展光电二极管探测器以支持多个传感器
- C++中的VLA,扩展名为std=C++11
- OpenGL 和 GLM 矩阵无法正确扩展,总是按比例缩小
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- C++返回 Numpy 数组的 Python 扩展模块
- 扩展可变参数模板中的变量名称
- 扩展C++生成的代码的模板参数类型名称
- 我想通过带有C++和Python的插件创建一个可扩展的应用程序
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- 将元组类型扩展为可变参数模板?
- 如何按文件扩展名引用文件夹中的文件
- HDF5Cpp 扩展复合数据集超板问题
- MSVC中的宏观扩展问题
- 嵌套参数包扩展失败
- C4204:使用的非标准扩展:非常量聚合初始值设定项
- [temp.variadic]中关于包扩展实例化的措辞
- 警告 C6031 返回值在宏扩展中被忽略