
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 – 模板参数推断错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)