C2143/C2518 尝试使用 boost.multiprecision 编译项目时

C2143/C2518 when trying to compile project using boost.multiprecision

我一直在尝试让 boost.multiprecision 在我的 VC2017 项目中工作时遇到问题,我试图使最简单的项目成为可能作为概念证明:

#include<boost/multiprecision/cpp_int.hpp>

int main() {
    boost::multiprecision::cpp_int val{ 5 };
    val *= 5;
    val *= 5;
    return val.convert_to<int>();
}

不幸的是,此代码无法编译,出现以下错误:

1>------ Build started: Project: Multiprecision Test, Configuration: Debug x64 ------
1>Multi Main.cpp
1>Unknown compiler version - please run the configure tests and report the results
1>g:\workspacec\solutions\project4x\library\include\boost\utility\compare_pointees.hpp(36): error C2143: syntax error: missing ',' before '<'
1>g:\workspacec\solutions\project4x\library\include\boost\utility\compare_pointees.hpp(40): note: see reference to class template instantiation 'boost::equal_pointees_t<OptionalPointee>' being compiled
1>g:\workspacec\solutions\project4x\library\include\boost\utility\compare_pointees.hpp(59): error C2143: syntax error: missing ',' before '<'
1>g:\workspacec\solutions\project4x\library\include\boost\utility\compare_pointees.hpp(63): note: see reference to class template instantiation 'boost::less_pointees_t<OptionalPointee>' being compiled
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(453): error C2143: syntax error: missing ',' before '<'
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(467): note: see reference to class template instantiation 'boost::numeric::convdetail::trivial_converter_impl<Traits>' being compiled
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(453): error C2518: keyword 'typename' illegal in base class list; ignored
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(454): error C2518: keyword 'typename' illegal in base class list; ignored
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(474): error C2143: syntax error: missing ',' before '<'
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(497): note: see reference to class template instantiation 'boost::numeric::convdetail::rounding_converter<Traits,RangeChecker,RawConverter,Float2IntRounder>' being compiled
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(474): error C2518: keyword 'typename' illegal in base class list; ignored
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(475): error C2518: keyword 'typename' illegal in base class list; ignored
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(504): error C2143: syntax error: missing ',' before '<'
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(526): note: see reference to class template instantiation 'boost::numeric::convdetail::non_rounding_converter<Traits,RangeChecker,RawConverter>' being compiled
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(504): error C2518: keyword 'typename' illegal in base class list; ignored
1>g:\workspacec\solutions\project4x\library\include\boost\numeric\conversion\detail\converter.hpp(505): error C2518: keyword 'typename' illegal in base class list; ignored
1>Done building project "Multiprecision Test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

这些与我在最初使用 boost.multiprecision 的更复杂的项目中遇到的错误完全相同。我在 Visual Studio 2015 年编译这段代码时没有遇到任何问题。有谁知道出了什么问题,我需要做什么来修复它?

编辑:

使用 boost.asio 的项目编译没有问题:

#include<boost/asio.hpp>
#include<iostream>

int main() {
    boost::asio::io_service service;
    for (int i = 0; i < 10; i++) {
        service.post([i] {
            std::cout << i << std::endl;
        });
    }
    service.run();
    system("pause");
    return 0;
}

问题是由于 boost::multiprecision 中的某些模板使用了 std::unary_function,自 C++11 以来已弃用并已从 C++17 的标准中删除。

MSVC 2015 中的标准库实现引入了诸如 #if _HAS_AUTO_PTR_ETC 之类的保护措施来围绕此类已弃用的定义。它们在新开关 /std:c++14 下默认设置为 1(默认值),在 /std:c++latest 下默认设置为 0(新编译器开关自 2015 年更新后可用) 3).

因此,在 boost 删除对 std::unary_function 的依赖之前,您必须要么不使用 /std:c++latest(自从它问世以来我一直在使用它),要么在之前使用 #define _HAS_AUTO_PTR_ETC 1包括(直接或间接)任何标准库头文件。因此,要么使用编译器选项设置它,要么在第一个包含在所有翻译单元或类似内容中的某些 PCH 中设置它。

可以在 this blog post by Stephan T. Lavavej. The Visual C++ change history 2003 - 2015 seems to be the official list of breaking changes in MSVC, but unfortunately it doesn't cover all these details. In general, scanning the Visual C++ Team Blog 中找到对这些设置的全面描述,包括控制其他已弃用或已删除功能的其他保护措施,因为 Stephan 的帖子将为您提供有关这些内容的最佳信息。