我试图绕过参考崩溃(https://www.amazon.com/Effective-Modern-Specific-Ways-Improve/dp/1491903996上的项目28 ),并想玩弄各种东西来模板。
我用左值和ravlue调用模板,但是在模板中看不到确切的类型。如何让编译器在专门用于模板的时间内吐出确切的类型?
#include <iostream>
#include <typeinfo>
class Widget{
public:
int x=0;
};
template<typename T>
void MyMethod(T&& param){
std::cout << typeid(param).name()<< std::endl; // this just says 6Widget...
};
Widget GetWidget(){
return Widget();
};
int main() {
Widget &w1 = * new Widget;
MyMethod(w1);
Widget w2;
MyMethod(w2);
MyMethod(GetWidget());
return 0;
}
这只是输出
6Widget
6Widget
6Widget
编译器有什么办法吐出它为调用模板所做的确切的专业化和类型?
真的很棒。好像您跳了很多章。
在第4项中,您将看到如何使用Boost.TypeIndex来做到这一点,并具有:
std::cout << boost::typeindex::type_id_with_cvr<decltype(param)>().pretty_name();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句