c – 模板参数推断错误

c – 模板参数推断错误,第1张

概述template <typename T>void foo(int i){ //nothing inside}int main(){ foo(5); //fails foo<int>(5); //works} 为什么foo(5)会失败但是foo< int>(5)工作? 你也许想写 template <typename T>void foo(T i)
template <typename T>voID foo(int i){  //nothing insIDe}int main(){   foo(5); //fails   foo<int>(5); //works}

为什么foo(5)会失败但是foo< int>(5)工作?

解决方法 你也许想写

template <typename T>voID foo(T i)          // note the type of i{  //nothing insIDe}

更新

以下是完整的代码

#include <iostream>using std::cout;template <typename T>voID foo( T i ) {    cout << __PRETTY_FUNCTION__ << " called with " << i << "\n";}int main() {    foo( 5 );    foo<int>( 7 );}

并输出:

voID foo(T) [with T = int] called with 5voID foo(T) [with T = int] called with 7
总结

以上是内存溢出为你收集整理的c – 模板参数推断错误全部内容,希望文章能够帮你解决c – 模板参数推断错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1213142.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存