繁体   English   中英

可以使用自动占位符在非类型模板参数中推导出函数结果吗?

[英]Can auto placeholder be used to deduce function result in non-type template parameter?

考虑简单的例子:

template <auto(*X)()>
struct Foo {
    decltype(X()) x;
};

int bar();

int main() {
    static_cast<void>(Foo<bar>{});
}

[gcc][clang]似乎都接受了代码。 代码真的符合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>{});
}

这个只让[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是在误差中的第二个例子:你的明确指定Tint正在执行扣除前取代的([temp.deduct] /2.3,/ 5,和/ 6,通过引用[dcl.type.auto.deduct] /2.3和/ 4)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM