c – 模板参数推断错误
发布时间:2020-12-16 09:30:10 所属栏目:百科 来源:网络整理
导读:template typename Tvoid foo(int i){ //nothing inside}int main(){ foo(5); //fails fooint(5); //works} 为什么foo(5)会失败但是foo int(5)工作? 解决方法 你也许想写 template typename Tvoid foo(T i) // note the type of i{ //nothing inside} 更新
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 5 void foo(T) [with T = int] called with 7 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |