gpt4 book ai didi

c++ - 在模板模板参数的情况下,由 decltype(auto) 推导的引用非类型模板参数是否可转发

转载 作者:可可西里 更新时间:2023-11-01 16:47:12 25 4
gpt4 key购买 nike

还有一个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 提交错误之前需要确认。我说得对吗?

最佳答案

根据错误,clang 在推导模板模板参数 时没有问题,它也符合标准 - [temp.arg.template]/3 (强调我的):

A template-argument matches a template template-parameter P when P is at least as specialized as the template-argument A. If P contains a parameter pack, then A also matches P if each of A's template parameters matches the corresponding template parameter in the template-parameter-list of P. Two template parameters match if they are of the same kind (type, non-type, template), for non-type template-parameters, their types are equivalent ([temp.over.link]), and for template template-parameters, each of their corresponding template-parameters matches, recursively. When P's template-parameter-list contains a template parameter pack, the template parameter pack will match zero or more template parameters or template parameter packs in the template-parameter-list of A with the same type and form as the template parameter pack in P (ignoring whether those template parameters are template parameter packs)

现在让我们确保 Bar<(x)>{}应推导为引用。这由 [dcl.type.auto.deduct]/5 涵盖和 [dcl.type.simple]/4 .

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

A template-argument for a non-type template-parameter shall be a converted constant expression of the type of the template-parameter. For a non-type template-parameter of reference or pointer type, the value of the constant expression shall not refer to (or for a pointer type, shall not be the address of):

  • a subobject,
  • a temporary object,
  • a string literal,
  • the result of a typeid expression, or
  • a predefined ­­func_­_­ variable.

[ Note: If the template-argument represents a set of overloaded functions (or a pointer or member pointer to such), the matching function is selected from the set ([over.over]). — end note  ]

推导的论证满足要求。这使得代码格式正确并提示 clang 的错误。

Filed bug 34690

关于c++ - 在模板模板参数的情况下,由 decltype(auto) 推导的引用非类型模板参数是否可转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46328005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com