warning C6031 return value ignored in macro expansion

穿精又带淫゛_ 提交于 2019-12-11 15:09:58

问题


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:

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;
}

Example file output for E_FAIL error code:

7:50:11 Unspecified error


回答1:


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())); }

to

#define LOG_IF_FAILED(file_name, line, hr) if constexpr (FAILED(hr)) \
    { TRACE(TEXT("%s %i %s"), file_name, line, _com_error(hr).ErrorMessage()); }

That is: { TRACE((..., ..., ..., ...)); } to { TRACE(..., ..., ..., ...); }

But I have to admit, that I don't know if there are some other unintended result of removing the extra parenthesis.



来源:https://stackoverflow.com/questions/58213233/warning-c6031-return-value-ignored-in-macro-expansion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!