c++ - 为什么会出现错误 "no matching function for call to ' A(A<...auto...> )'"?

标签 c++ templates gcc c++20 template-argument-deduction

在 C++20 中,我们有更好的 NTTP,它允许字面量类类型:

template <typename T>
struct A{};

template <A> // note: it is a placeholder for NTTP A<T>
struct B{};
但是当我尝试为 B 专门化一个模板时:
template <typename>
struct is_B{};

template <A x>
struct is_B<B<x>>{}; // error!!!
编译器(GCC 10 或 GCC 11(trunk))转储了一堆错误:
prog.cc:11:15: error: class template argument deduction failed:
   11 | struct is_B<B<x>>{};
      |               ^
prog.cc:11:15: error: no matching function for call to 'A(A<...auto...>)'
prog.cc:2:8: note: candidate: 'template<class T> A()-> A<T>'
    2 | struct A{};
      |        ^
prog.cc:2:8: note:   template argument deduction/substitution failed:
prog.cc:11:15: note:   candidate expects 0 arguments, 1 provided
   11 | struct is_B<B<x>>{};
      |               ^
prog.cc:2:8: note: candidate: 'template<class T> A(A<T>)-> A<T>'
    2 | struct A{};
      |        ^
prog.cc:2:8: note:   template argument deduction/substitution failed:
prog.cc:11:15: note:   mismatched types 'A<T>' and 'A<...auto...>'
   11 | struct is_B<B<x>>{};
      |               ^
prog.cc:11:16: error: template argument 1 is invalid
   11 | struct is_B<B<x>>{};
      |                ^~
我找到了一个解决方案,它为 A 提供了一个明确的参数。 :
template <typename T, A<T> x>
struct is_B<B<x>>{};
但是它是多余的,并且无法解决A时相同的错误。用于using :
template <A x>
using B_t = B<x>; // same error!!!
那么还有其他解决方案吗?

最佳答案

似乎 GCC 不喜欢作为 NTTP 推断类类型的占位符,应该按照 [temp.param]/6 接受。 (强调我的):

A non-type template-parameter shall have one of the following (possibly cv-qualified) types:

  • a structural type (see below),
  • a type that contains a placeholder type ([dcl.spec.auto]), or
  • a placeholder for a deduced class type ([dcl.type.class.deduct]).

已经有一个相关的错误报告(PR96331,参见@TC 提供的示例)。

关于c++ - 为什么会出现错误 "no matching function for call to ' A(A<...auto...> )'"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64567607/

相关文章:

c++ - boolean 值的大小是多少? 1 位还是 1 字节?

c++ - 将 QString 与 wchar_t 一起使用时未解析的外部符号

iphone - 更新到 lion 和 xCode 后内存集崩溃

templates - 在 Go 的 HTML 模板中遍历任意数量的嵌套结构 slice

c++ - Enable_if 作为模板参数

没有循环的 C 程序意外地表现得像一个循环

c++ - 如何禁止Qt Creator自动使用特定的包含路径?

c - arm-linux-gnueabi-gcc 找不到库

c++ - 在运行时将 char 数组转换为 String?

c++ - 为模板特化选择字符串文字类型