繁体   English   中英

使用默认模板参数在 function 模板上获取具有自动推导返回类型的指针

[英]Getting a pointer with auto deduced return type on function template with default template argument

以下程序使 function g<void>()上的指针x具有自动推断的返回类型:

template<class=void>
void g() {}

int main() {
    auto (*x)() = &g;
    (*x)();
}

该程序被 GCC 接受,但在 Clang 中被拒绝,错误如下:

error: variable 'x' with type 'auto (*)()' has incompatible initializer of type '<overloaded function type>'
    auto (*x)() = &g;

演示: https://gcc.godbolt.org/z/s17Mf74Wc

哪个编译器在这里?

通过 [dcl.type.auto.deduct] 在变量声明中对占位符auto的推导就像通过发明的 function 模板的模板参数推导一样完成

template<typename T>
void f(T (*x)());

称为f(&g)

[temp.deduct.call] 表示在模板参数推导中,如果参数是 function 或指向函数的类型,并且参数是包含至少一个 function 模板的重载集,则将其视为非推导上下文。

所以在我看来,扣除应该在这里失败。

暂无
暂无

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

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