标签:style sp on 问题 代码 amp ef as size
在《C++ Primer》第五版中,关于模版实参推断,讲述了如下内容
1.类型转换与模版类型参数
2.函数模版的显式实参
3.尾置返回类型
4.类型转换
5.函数指针和实参推断
6.模版参数推断和引用
书中留给大家一些习题,要求大家给出模版参数的推断类型,参见16.2.5节练习。问题是如何验证我们的答案是否正确,我在这里给出一段代码,抛砖引玉!
<span style="font-size:14px;">template <typename T> void g(T&& val) { if(is_lvalue_reference<T>::value) { if(is_const<typename remove_reference<T>::type>::value){cout<<"const left reference of "<<typeid(T).name()<<endl;} else{cout<<"left reference of "<<typeid(T).name()<<endl;} } else if(is_rvalue_reference<T>::value) { if(is_const<typename remove_reference<T>::type>::value){cout<<"const right reference of "<<typeid(T).name()<<endl;} else{cout<<"right reference of "<<typeid(T).name()<<endl;} } else cout<<typeid(T).name()<<endl; } //------------------------------------------------------------ </span><pre name="code" class="cpp"><span style="font-size:14px;">template <typename T> void g(const T& val) { if(is_lvalue_reference<T>::value) { if(is_const<typename remove_reference<T>::type>::value){cout<<"const left reference of "<<typeid(T).name()<<endl;} else{cout<<"left reference of "<<typeid(T).name()<<endl;} } else if(is_rvalue_reference<T>::value) { if(is_const<typename remove_reference<T>::type>::value){cout<<"const right reference of "<<typeid(T).name()<<endl;} else{cout<<"right reference of "<<typeid(T).name()<<endl;} } else cout<<typeid(T).name()<<endl; } //--------------------------------------------------------------- </span><pre name="code" class="cpp"><pre name="code" class="cpp"><span style="font-size:14px;">template <typename T> void g(T val) { if(is_lvalue_reference<T>::value) { if(is_const<typename remove_reference<T>::type>::value){cout<<"const left reference of "<<typeid(T).name()<<endl;} else{cout<<"left reference of "<<typeid(T).name()<<endl;} } else if(is_rvalue_reference<T>::value) { if(is_const<typename remove_reference<T>::type>::value){cout<<"const right reference of "<<typeid(T).name()<<endl;} else{cout<<"right reference of "<<typeid(T).name()<<endl;} } else cout<<typeid(T).name()<<endl; }</span>
<span style="font-size:14px;">int main() { double i=0;const double ci=i; g(i); g(ci); g(i*ci); g(i=ci); return 0; } </span>关于更多的内容我就不照本宣科了,大家可以自己翻阅相关书籍。
标签:style sp on 问题 代码 amp ef as size
原文地址:http://blog.csdn.net/zjq2008wd/article/details/41354569