W.F*_*.F. 9 c++ templates language-lawyer c++17
考虑简单的例子:
template <auto(*X)()>
struct Foo {
decltype(X()) x;
};
int bar();
int main() {
static_cast<void>(Foo<bar>{});
}
Run Code Online (Sandbox Code Playgroud)
无论[GCC]和[铛]似乎接受的代码.代码真的符合c ++ 17吗?如果是这样,还有其他一些规则会使下面的代码生成错误吗?
template <class T, auto(*X)(T)>
struct Foo {
decltype(X(0)) x;
};
int bar(int);
int main() {
static_cast<void>(Foo<int, bar>{});
}
Run Code Online (Sandbox Code Playgroud)
这个只让[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'
Run Code Online (Sandbox Code Playgroud)
是的,auto可以在复合类型内使用([temp.param]/4.6、[dcl.type.auto.deduct])。我相信 gcc 在你的第二个例子中是错误的:在执行推导之前明确指定的T被int替换([temp.deduct]/2.3、/5和/6,由[dcl.type.auto.deduct]/2.3引用和/4)。