显示当前(隐性)扣除指南

发布于 2025-02-06 11:07:15 字数 777 浏览 1 评论 0 原文

我有以下这个问题中的错误消息,类别的类模板指南基于基于参数

<source>:44:8: error: template parameters not deducible in partial specialization:
   44 | struct A3<int> : A<N> {
      |        ^~~~~~~
<source>:44:8: note:         'N'
Compiler returned: 1

如何查看 a3 a 的扣除指南?

编辑:删除了无效的摘录。好吧,评论讨论变得非常糟糕。 用于生成隐式推力指南的规则 可能很复杂,尤其是聚集的。我认为能够看到自动生成的任何指南将是有用的,尤其是如果您不熟悉C ++并想调整隐式指南。这就是为什么这个问题很重要的原因,即使摘录无效。

I have the following error message from this question, class template deducation guide for struct-based parameters.

<source>:44:8: error: template parameters not deducible in partial specialization:
   44 | struct A3<int> : A<N> {
      |        ^~~~~~~
<source>:44:8: note:         'N'
Compiler returned: 1

How can I view the deduction guides for A3 and A?

Edit: removed invalid excerpt. Well, the comment discussion got horribly sidetracked. The rules for generating implicit deduction guides can be complicated, especially for aggregates. I think it would be useful to be able to see any guides which have been automatically generated, especially if you are new to C++ and want to tweak the implicit guides. That's why this question is important even if the excerpt was invalid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2025-02-13 11:07:15

您可以从Clang生成AST树。它显示了诸如隐式声明为特殊方法(默认构造函数等)以及隐式扣除指南之类的事物。然而,扣除指南似乎只有在实际被用来推断模板时才显示。我不熟悉该工具,无法告诉您是否有办法向它们展示。

例如,对于此代码:

template <class T>
struct A
{
  A(T a) : a_{a} {}
  T a_;
};

auto test()
{
    auto a = A{24}; // without this the deduction guides are not shown
}

这是相关输出:

  translationunitdecl
|
| ...
|
| -FunctionTemplatedEcl&lt;线:1:1,线:4:19&GT; col:3 IntiNit&lt; a&gt的扣除指南;
| | -templatetepepeparmdecl&lt; line:1:11,col:17&gt; col:17引用的类深度0索引0 t
| | -cxxdeductionGuidedeCl&lt;线:4:3,col:19&gt; col:3 IntiNit&lt; a&gt的扣除指南; 'auto(t) - &gt; a&lt; t&gt;'
| | `-parmvardecl&lt; col:5,col:7&gt; col:7 a't'
| `-cxxDeductionGuidedeCl&lt; col:3,col:19&gt; col:3含义的A&gt的扣除指南; 'auto(int) - &gt; a&lt; int&gt;'
| | -templateargument类型'int'
| | `-builtIntype'int'
| `-parmvardecl&lt; col:5,col:7&gt; col:7 a'int':'int'
`-functionTemplatedEcl&lt; line:1:1,line:2:8&gt; col:8 IntiNit&lt; a&gt的扣除指南;
  | -templatetepepeparmdecl&lt; line:1:11,col:17&gt; col:17引用的类深度0索引0 t
  `-cxxDeductionGuidedeCl&lt; line:2:8&gt; col:8 IntiNit&lt; a&gt的扣除指南; 'auto(a&lt; t&gt;) - &gt; a&lt; t&gt;'
    `-parmvardecl&lt; col:8&gt; col:8'a&lt; t&gt;'
 

您可以看到a&gt; auto(t) - &gt; a&lt; t&gt; 的“隐式&lt;扣除指南”。

玩它”或 generate it it 自己。

另一个工具是 cppinsight (也是由clang构建的)。它仅显示扣除指南的“专业化”:

 / *首先从:Insights.cpp:10 */
#ifdef Insights_use_template
模板&lt;&gt;
a(int a) - &gt; a&lt; int&gt;;
#endif
 

You can generate the AST tree from clang. It shows a bunch on things like implicitly declared special methods (default constructors etc.) as well as implicit deduction guides. The deduction guides however seem to be shown only when they are actually used to deduce a template. I am not familiar with the tool and couldn't tell you if there is a way to show them regardless.

E.g. for this code:

template <class T>
struct A
{
  A(T a) : a_{a} {}
  T a_;
};

auto test()
{
    auto a = A{24}; // without this the deduction guides are not shown
}

This is the relevant output:

TranslationUnitDecl
|
| ...
|
|-FunctionTemplateDecl <line:1:1, line:4:19> col:3 implicit <deduction guide for A>
| |-TemplateTypeParmDecl <line:1:11, col:17> col:17 referenced class depth 0 index 0 T
| |-CXXDeductionGuideDecl <line:4:3, col:19> col:3 implicit <deduction guide for A> 'auto (T) -> A<T>'
| | `-ParmVarDecl <col:5, col:7> col:7 a 'T'
| `-CXXDeductionGuideDecl <col:3, col:19> col:3 implicit used <deduction guide for A> 'auto (int) -> A<int>'
|   |-TemplateArgument type 'int'
|   | `-BuiltinType 'int'
|   `-ParmVarDecl <col:5, col:7> col:7 a 'int':'int'
`-FunctionTemplateDecl <line:1:1, line:2:8> col:8 implicit <deduction guide for A>
  |-TemplateTypeParmDecl <line:1:11, col:17> col:17 referenced class depth 0 index 0 T
  `-CXXDeductionGuideDecl <line:2:8> col:8 implicit <deduction guide for A> 'auto (A<T>) -> A<T>'
    `-ParmVarDecl <col:8> col:8 'A<T>'

You can see "implicit <deduction guide for A > auto (T) -> A<T>".

Play with it on godbolt or generate it yourself.

Another tool is cppinsight (which is built from clang also). It only shows the "specialization" of the deduction guide if it's used:

/* First instantiated from: insights.cpp:10 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
A(int a) -> A<int>;
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文