Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find template parameters at runtime

Tags:

c++

templates

it's possible to have access to template "type", for instance in the std

std::vector<int>::size_type

is it possible to have the same thing for objects passed as template parameters? For instance:

template<int i>
class A {
//?
};

A<3> instance;
int number = instance::???? //<--- assigns 3 to number

is it possible to get the 3 passed in the object type again at runtime? Without creating a specific member in the A class (which would increase the size of the object)

thanks

like image 599
lezebulon Avatar asked May 02 '26 06:05

lezebulon


1 Answers

The type of the variable is known to the compiler at compile time, it's just a matter of getting it to give it up.

template<int i>
int get(const A<i> & instance)
{
    return i;
}
like image 86
Mark Ransom Avatar answered May 03 '26 20:05

Mark Ransom



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!