问题描述
考虑简单的示例:
template <auto(*X)()>
struct Foo {
decltype(X()) x;
};
int bar();
int main() {
static_cast<void>(Foo<bar>{});
}
[gcc] 和 [c] 似乎接受该代码.该代码真的符合c ++ 17吗?如果是这样,还有其他规则会使下面的代码不正确吗?
Both [gcc] and [clang] seem to accept the code. Is the code really c++17 compliant? If so is there some other rule that makes the following code ill formed?
template <class T, auto(*X)(T)>
struct Foo {
decltype(X(0)) x;
};
int bar(int);
int main() {
static_cast<void>(Foo<int, bar>{});
}
这仅使 [gcc] 不满意.
错误消息:
prog.cc: In function 'int main()':
prog.cc:9:35: error: unable to deduce 'auto (*)(T)' from 'bar'
static_cast<void>(Foo<int, bar>{});
^
prog.cc:9:35: note: mismatched types 'T' and 'int'
推荐答案
是的,可以使用auto
( [temp.param] /4.6 , [dcl.type.auto.deduct] ).我相信gcc在您的第二个示例中是错误的:在执行扣除之前,已替换显式指定的int
的T
([temp.deduct]/2.3、/5和/6,由[dcl.type引用. auto.deduct]/2.3和/4).
Yes, auto
may be used inside a compound type ([temp.param]/4.6, [dcl.type.auto.deduct]). I believe that gcc is in error in your second example: your explicitly specified T
of int
is substituted before performing deduction ([temp.deduct]/2.3, /5, and /6, referenced by [dcl.type.auto.deduct]/2.3 and /4).
这篇关于自动占位符可用于推导非类型模板参数的函数结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!