gpt4 book ai didi

c++ - g++7.2.0 自动推导非类型参数失败

转载 作者:行者123 更新时间:2023-11-28 01:39:16 25 4
gpt4 key购买 nike

我正在尝试使用自动非类型参数 (c++17)。我预计“Sample1::type”应该是“integral_constraint ”,但它与“Sample0::type”相同。

是 g++ 错误还是我对该功能的误解?我在 Ubuntu 17.10 上使用 g++ (Ubuntu 7.2.0-8ubuntu3) 7.2.0。

-- auto_param.cxx --
#include <iostream>
#include <type_traits>
#include <boost/type_index.hpp>

template <auto V>
struct Value {
using type = std::integral_constant<decltype(V), V>;
};

template <typename T>
auto demangle() {
return boost::typeindex::type_id_with_cvr<T>().pretty_name();
}

void zero_as_uint() {
using Sample0 = Value<0u>;
std::cout << __func__ << ": " << demangle<Sample0::type>() << std::endl;
}

void zero_as_int() {
using Sample1 = Value<0>;
std::cout << __func__ << ": " << demangle<Sample1::type>() << std::endl;
}

int main(int, char**) {
zero_as_uint();
zero_as_int();
return 0;
}
-----------------------------
$ g++ -Wall -std=c++17 auto_param.cxx && ./a.out
zero_as_uint: std::integral_constant<unsigned int, 0u>
zero_as_int: std::integral_constant<unsigned int, 0u>

我发现 clang++ 的推导符合我的预期。

$ clang++-5.0 -Wall -std=c++17 auto_param.cxx && ./a.out
zero_as_uint: std::integral_constant<unsigned int, 0u>
zero_as_int: std::integral_constant<int, 0>

最佳答案

那是一个 bug in gcc .进一步简化的示例如下所示:

#include <type_traits>

template <auto V>
struct Value {
using type = std::integral_constant<decltype(V), V>;
};

static_assert(!std::is_same_v<Value<0u>::type, Value<0>::type>);

这两种类型不可能相同,因为它们引用不同的模板实例化。看起来像 gcc has always had the bug , 但在最新的trunk中是固定的。

关于c++ - g++7.2.0 自动推导非类型参数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48011725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com