尝试使用 boost.multi precision 编译项目时出现 C2143/C2518

2024-05-15

我在尝试让 boost.multi precision 在我的 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.multi precision 的更复杂的项目中遇到的错误完全相同。我在 Visual Studio 2015 中编译此代码没有遇到任何问题。有谁知道出了什么问题,以及我需要做什么来修复它?

EDIT:

使用 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 use std::unary_function,自 C++11 起已弃用,并已从 C++17 标准中删除。

MSVC 2015 中的标准库实现引入了类似的守卫#if _HAS_AUTO_PTR_ETC围绕这些已弃用的定义。它们被设置为1默认情况下在新开关下/std:c++14(默认)并设置为0默认情况下/std:c++latest(新的编译器开关自 2015 Update 3 起可用)。

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

这些设置的完整描述,包括控制其他已弃用或删除的功能的其他防护,可以在这篇博文 https://blogs.msdn.microsoft.com/vcblog/2016/08/12/stl-fixes-in-vs-2015-update-3/作者:Stephan T. Lavavej。这Visual C++ 变化历史 2003 - 2015 https://msdn.microsoft.com/en-us/library/bb531344(v=vs.140).aspx似乎是 MSVC 中重大更改的官方列表,但不幸的是它没有涵盖所有这些细节。一般来说,扫描Visual C++ 团队博客 https://blogs.msdn.microsoft.com/vcblog/斯蒂芬的帖子将为您提供有关这些事情的最佳信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试使用 boost.multi precision 编译项目时出现 C2143/C2518 的相关文章