我正在尝试使用auto非类型参数(c ++ 17)。我希望'Sample1 :: type'应该是'integral_constraint <int,0>',但它与'Sample0 :: type'相同。
它是g ++ bug还是我对这个功能的误解?我在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,但它是在最新的主干中修复的。