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

g++7.2.0 fails auto non-type parameter deduction

我正在尝试使用自动非类型参数 (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>);

这两种类型不可能相同,因为它们引用不同的模板实例化。貌似是gcchas always had the bug,不过在最新的trunk里已经修复了