jeudi 4 mars 2021

Using decltype to get type of object and then find which template is used in type [duplicate]

Sorry if this is a duplicate but I have a hard time finding the right search words so I would be happy with a redirect.

I have a template class as such:

template <typename T1>
class my_class {
//stuff
}

Now somewhere else I have a function that returns a myclass but I'm using auto so I don't know the type. I would like to get to know the T1. So something like this:

auto my_class_object = function(...);
decltype(my_class_object)::T1 other_variable;

This second line doesn't work but is there something to achieve this behaviour (with c++11)?

One solution I thought of which I haven't tested because I don't like it too much is:

template <typename T1>
class my_class {
typedef T1 template_type1
//stuff
}

and then use:

auto my_class_object = function(...);
decltype(my_class_object)::template_type1 other_variable;

I'm hoping there exists something more elegant than this solution but if not I'll just go for that.

Aucun commentaire:

Enregistrer un commentaire