首页 > 解决方案 > 启用 c++17 时 __cplusplus 的奇怪输出

问题描述

我在这里用元编程做一些实验,最初的想法是找出我们是否可以在编译时使排序的整数序列唯一。(虽然这与问题无关)

我在我的属性中启用了 c++17(我使用 VS2019),当我想找出 C++ 标准时,这个程序给了我奇怪的输出。

#include <iostream>
#include <boost/mp11.hpp>
#include <type_traits>

namespace
{
    template <int... N>
    using seq = boost::mp11::mp_list_c<int, N...>;

    template <int... N>
    struct uniq
    {
        using type = boost::mp11::mp_unique<seq<N...>>;
    };
}

int main()
{
    static_assert(std::is_same_v<uniq<1, 2, 2, 2, 3, 3, 3>::type, seq<1, 2, 3>>,"Assertion Failed");
    static_assert(std::is_same_v<uniq<4, 1, 9, 9, 2, 2, 3, 1, 5>::type, seq<4, 1, 9, 2, 3, 5>>,"Assertion Failed");

    if (__cplusplus == 201703L) std::cout << "C++17\n";
    else if (__cplusplus == 201402L) std::cout << "C++14\n";
    else if (__cplusplus == 201103L) std::cout << "C++11\n";
    else if (__cplusplus == 199711L) std::cout << "C++98\n";
    else std::cout << "pre-standard C++\n";
    return 0;
}

这会打印“C++98”,我想知道这是怎么回事?我已经从属性中启用了 c++17,并且我显然使用了一些比 c++98 更高级的语言功能(如可变参数模板)。
这会给任何人敲响警钟吗?我会想念什么吗?

标签: c++c++17language-lawyer

解决方案


在 Visual Studio 中,您必须启用/Zc:__cplusplus才能获得适当的值。

这与向后兼容性有关。


推荐阅读