简体   繁体   English

Function 模板参数推导在 gcc 中有效,但在 msvc 和 clang 中无效

[英]Function template argument deduction works in gcc but not in msvc and clang

I am learning C++ templates using the resource listed here .我正在使用此处列出的资源学习 C++ 模板。 In particular, read about template argument deduction .特别是,阅读模板参数推导 Now, after reading, to further clear up my concept of the topic I tried the following example that compiles in gcc but not in clang and msvc.现在,阅读后,为了进一步理清我对主题的概念,我尝试了以下示例,该示例在 gcc 中编译,但在 clang 和 msvc 中编译。 Demo演示

template<typename T = int> void f()
{

}
template<typename T> void func(T)
{

}
int main()
{
    func(f); //works in gcc but not in clang and msvc 
    func(f<>); //works in all
}

As we can see, the above example compiles fine in gcc but not in clang and msvc.正如我们所见,上面的示例在 gcc 中编译得很好,但在 clang 和 msvc 中编译得不好。 My question is which compiler is right here according to the latest standard?我的问题是根据最新标准,这里有哪个编译器?

This is CWG 2608 and the program is well-formed so that gcc is correct in accepting the program.这是CWG 2608并且程序格式正确,因此gcc 在接受程序时是正确的。


From temp.arg.explicit#4 which was added due to cwg 2608 , the default template argument can be used and the empty template argument list <> can be omitted.从由于 cwg 2608添加的temp.arg.explicit#4中,可以使用默认模板参数,并且可以省略空模板参数列表<>

If all of the template arguments can be deduced or obtained from default template-arguments , they may all be omitted;如果模板 arguments 都可以从默认的模板参数中推导出或得到,则可以全部省略; in this case, the empty template argument list <> itself may also be omitted.在这种情况下,空模板参数列表 <> 本身也可以省略。

(emphasis mine) (强调我的)

Note the bold highlighted part in the above quoted statement, which means that the empty template argument list <> can be omitted in your example because all of the template arguments can be obtained from default template-arguments.注意上面引用语句中的粗体突出部分,这意味着在您的示例中可以省略空模板参数列表<> ,因为所有模板 arguments 都可以从默认模板参数中获得。

Further, over.over#3 can also be used here to see that the specialization generated from temp.arg.explicit is added to the overloaded set:此外,这里也可以使用over.over#3来查看从temp.arg.explicit生成的特化被添加到重载集合中:

The specialization, if any, generated by template argument deduction ([temp.over], [temp.deduct.funcaddr], [temp.arg.explicit]) for each function template named is added to the set of selected functions considered.对于每个命名的 function 模板,由模板参数推导([temp.over]、[temp.deduct.funcaddr]、[temp.arg.explicit])生成的特化(如果有)被添加到所考虑的选定函数集中。

(emphasis mine) (强调我的)

This means that at the end, the call func(f);这意味着最后,调用func(f); is well-formed and uses the generated specialization f<int> .格式正确并使用生成的特化f<int>

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

相关问题 函数指针数组的类模板参数推论适用于clang,但不适用于gcc - Class template argument deduction for array of function pointer works on clang, but not on gcc GCC的模板模板参数推导失败(与MSVC一起使用) - Template template argument deduction failure with GCC (works with MSVC) Class 模板参数推导 - clang 和 gcc 不同 - Class Template Argument Deduction - clang and gcc differ 带有默认参数的 SFINAE 适用于 msvc 但不适用于 gcc 和 clang - SFINAE with default argument works with msvc but not with gcc and clang 具有可变参数包的函数的C ++部分模板参数推导在Clang和MSVC中产生不明确的调用 - C++ partial template argument deduction for function with variadic pack produces ambiguous call in Clang and MSVC 变量模板作为模板参数:演绎适用于GCC但不适用于Clang - Variadic template as template parameter: deduction works with GCC but not with Clang 匿名临时和类模板参数演绎 - gcc vs clang - Anonymous temporaries and class template argument deduction - gcc vs clang 为什么这个模板参数推导在 GCC 而不是 Clang 上失败? - Why does this template argument deduction fail on GCC but not Clang? Consexpr function 对模板参数 object 的评估(MSVC 与 clang/gcc) - Constexpr function evaluation on a template parameter object (MSVC vs clang/gcc) MSVC上的模板参数推断失败:bug? - Template argument deduction fails on MSVC: bug?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM