【问题标题】:Are reference non-type template parameters deduced by decltype(auto) forwardable in case of template template parameter在模板模板参数的情况下,由 decltype(auto) 推导出的引用非类型模板参数是否可转发
【发布时间】:2017-09-20 17:09:00
【问题描述】:

又一个decltype(auto)模板模板参数问题。这次我能够创建的用于重现错误的最少代码如下所示:

template <template <decltype(auto)> class TT, decltype(auto) V>
void foo(TT<V>) {
};

template <decltype(auto)>
struct Bar{};

int x;

int main() {
    foo(Bar<(x)>{});
}

[clang] 中的这会导致:

prog.cc:11:5: error: no matching function for call to 'foo'
    foo(Bar<(x)>{});
    ^~~
prog.cc:2:6: note: candidate template ignored: substitution failure [with TT = Bar]: non-type template argument is not a constant expression
void foo(TT<V>) {
     ^
1 error generated.

[gcc] 接受代码。

据我了解,代码格式正确,clang 的解释有问题,但在向 lvvm 提交错误之前需要确认。我说的对吗?

【问题讨论】:

  • @StoryTeller 我明白你的意思。回到电脑后,我会尝试编辑问题以减少暗示性
  • @StoryTeller 我不明白你的评论。 TT 是一个模板模板参数,可以推导出为 Bar 就好了? clang 的问题在于推导V
  • @Barry - 我的评论是关于 mcve。我可以发誓 TT 是一个普通的decltype(auto)。不是模板。显然我只是看错了。
  • 更简单的例子:让foo 成为template &lt;decltype(auto) V&gt; void foo(Bar&lt;V&gt; ) { } - 相同的行为,只是少了一个变量
  • @Barry:我认为这几乎将它固化为一个clang错误,是吗?

标签: c++ templates language-lawyer c++17 template-templates


【解决方案1】:

根据错误,clang 推导模板没有问题 template-parameter 也符合标准 - [temp.arg.template]/3(empasis mine):

当 P 为时,模板参数匹配模板模板参数 P 至少与模板参数 A 一样专业。如果 P 包含一个 参数包,那么如果 A 的每个模板,A 也匹配 P 参数匹配相应的模板参数 P 的模板参数列表。两个模板参数匹配,如果它们 属于同一类型(类型、非类型、模板),对于非类型 模板参数,它们的类型是等价的([temp.over.link]), 对于模板模板参数,它们对应的每一个 模板参数匹配,递归。当P template-parameter-list 包含一个模板参数包, 模板参数包将匹配零个或多个模板参数或 模板参数包在 A 的模板参数列表中,带有 与 P 中的模板参数包相同的类型和形式(忽略 那些模板参数是否是模板参数包)

现在让我们确保推导出 Bar&lt;(x)&gt;{} 作为参考。这由[dcl.type.auto.deduct]/5[dcl.type.simple]/4 覆盖。

最后让我们检查一下我们是否真的可以使用对带有链接的变量的引用作为模板参数[temp.arg.nontype]/2

非类型模板参数的模板参数应该是 模板参数类型的转换常量表达式。 对于引用或指针类型的非类型模板参数, 常量表达式的值不应引用(或指针 类型,不得为):

的地址
  • 子对象,
  • 一个临时对象,
  • 字符串文字,
  • typeid 表达式的结果,或
  • 一个预定义的 func__ 变量。

[ 注意:如果模板参数代表一组重载 函数(或指向此类的指针或成员指针),匹配 从集合中选择函数 ([over.over])。 — 尾注 ]

推导的参数满足要求。这使得代码格式良好并提示了 clang 的错误。

Filed bug 34690

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多