在模板参数列表中使用 decltype 来推断指向类成员的指针的类型

Use decltype inside template parameter list to deduce the type of pointer to class member

本文关键字:成员 类型 指针 参数 列表 decltype      更新时间:2023-10-16

当我尝试实例化这样的模板时,GCC 给了我一个"模板参数 2 无效"错误(请参阅using Check行(。我很好奇,我可以在模板参数列表之外使用decltype获得指向成员的指针,但不能在模板参数列表中(请参阅变量p2的定义(。事实上,Clang确实编译了这段代码。这是 GCC 中的一个错误,还是这段代码实际上是无效的,而 Clang 只是太包容了?

template <class T, T t> struct checker_template {};
struct S { int n; };
int main() {
  S s;
  constexpr auto p1 = &S::n;
  constexpr auto p2 = &decltype(s)::n;
  using Check = checker_template<int S::*, &decltype(s)::n>;
  return 0;
}

答案由T.C.提供。这确实是 GCC 中的一个错误。