使用 Boost 1.75 编译的 Visual Studio 2019 中的警告

Warnings in Visual Studio2019 compiling with Boost 1.75

刚刚升级 Visual Studio 并对我的项目进行了完全重新编译。现在得到一些关于提升的警告:

5>D:\My Libraries\Boost\boost_1_75_0\boost\concept_check.hpp(355,12): warning C4834: discarding return value of function with 'nodiscard' attribute
5>D:\My Libraries\Boost\boost_1_75_0\boost\concept_check.hpp(354): message : while compiling class template member function 'void boost::BinaryFunction<Func,Return,First,Second>::test(boost::false_type)'
5>        with
5>        [
5>            Func=std::less<CString>,
5>            Return=bool,
5>            First=ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>,
5>            Second=ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>
5>        ]

我现在看到几个以前没有出现的编译器警告。

我应该澄清一下,这些警告仅出现在 Release x86 构建 (MFC Unicode MD) 中。 x64 Release 构建正常。

这是一个已知问题,最新版本的 Visual Studio 向 std::equal_to 添加了 [[nodiscard]] 属性。该问题已提交到 boost 已经 https://github.com/boostorg/concept_check/issues/31.

暂时可以自己临时修改boost/concept_check.hpp文件来修复

  BOOST_concept(BinaryFunction,(Func)(Return)(First)(Second))
  {
      BOOST_CONCEPT_USAGE(BinaryFunction) { test(is_void<Return>()); }
   private:
      void test(boost::false_type)
      {
          (void)f(first,second); // add (void) here to silence the warning
          Return r = f(first, second); // require operator()
          (void)r;
      }

或者您可以在包含该文件之前添加 #pragma warning(disable : 4834) 以禁用该警告。