gjvdkamp
gjvdkamp

Reputation: 10516

Show type of param in template

I'm trying to get my head around reference collapsing (item 28 on https://www.amazon.com/Effective-Modern-Specific-Ways-Improve/dp/1491903996), and want to play around with feeding different things to a template.

I call the template with lvalue and ravlue, but I don't see the exact types in the template. How can I get the compiler to spit out the exact types for the times it specializes the template?

#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;
}

This just outputs

6Widget
6Widget
6Widget

Is there any way for the compiler to spit out the exact specializations and types it made for the calls to the template?

Upvotes: 1

Views: 42

Answers (1)

Ranoiaetep
Ranoiaetep

Reputation: 6647

Really great book. Seems like you have jumped many chapters though.

In Item 4, you would see how you could do that with Boost.TypeIndex, with:

std::cout << boost::typeindex::type_id_with_cvr<decltype(param)>().pretty_name();

Upvotes: 3

Related Questions