1

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.

C. Binair
  • 419
  • 4
  • 14
  • 3
    No you can't use template arguments other than from inside the template itself. And the solution you don't like is actually the most common solution and is used all over the standard library. – Some programmer dude Mar 04 '21 at 09:23
  • 2
    Oke thanks. I actually also just found this answer https://stackoverflow.com/a/5056695/10220019 which proposes the same solution as I have. I was just hoping something better would exist, but thanks! – C. Binair Mar 04 '21 at 09:24
  • I added a close vote for the duplicate you found. – Scheff's Cat Mar 04 '21 at 09:26

0 Answers0