【问题标题】:Why is there an error "no matching function for call to 'A(A<...auto...>)'"?为什么会出现错误“调用 'A(A<...auto...>)' 没有匹配的函数”?
【发布时间】:2021-02-10 12:13:24
【问题描述】:

在 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>>{};

但它是多余的,并且不能解决Ausing中使用时的相同错误:

template <A x>
using B_t = B<x>; // same error!!!

那么还有其他解决方案吗?

【问题讨论】:

  • 您的代码现在可以使用 GCC 11 20201207 进行编译。看来他们已经修复了这个错误。不过,不知道是什么补丁修复了它,或者它是否被反向移植到 GCC10 分支。
  • 我想我明白了。如果我没记错的话,它已经被这个提交向后移植到 GCC 10:3e7a892cc5bdeb5518888f2ca176d96cc720ec5f.

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


【解决方案1】:

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

非类型模板参数应具有以下之一 (可能是 cv 限定的)类型:

  • 一种结构类型(见下文),
  • 包含占位符类型 ([dcl.spec.auto]) 的类型,或
  • 一个推导类类型的占位符 ([dcl.type.class.deduct])。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2018-11-29
    • 1970-01-01
    • 2019-02-13
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多