Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access template parameter from class object

I have a class template in myclass.hpp:

template<class T, class P>
class myclass
{
....
};

In my main.cc I create an object of the class:

myclass<int, double> mc;
otherfunc<myclass>(mc);

In some other header file header1.hpp:

template<class MyClass>
void otherfunc(MyClass const &mc)
{
/* Access through 'mc' the underlying template parameters T and P*/
}

How can I access the template parameter T and P in header1.hpp?

like image 809
Vinayak Gholap Avatar asked Jul 14 '26 22:07

Vinayak Gholap


1 Answers

How can I access the template parameter T and P in header1.hpp?

Provide public type definitions in your class myclass:

template<class T, class P>
class myclass
{
public:
     typedef T T_type;
     typedef P P_type;
....
};

Thus you can access those types as

typename myclass::T_Type x;
typename myclass::P_Type y;

elsewhere.

like image 64
πάντα ῥεῖ Avatar answered Jul 16 '26 10:07

πάντα ῥεῖ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!