Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable template in C++11

Maybe I will have a stupid question. The C++14 standard brings a concept of a template variable. Is it possible to do a similar thing using the C++11 standard?

Thank you, Marek

like image 350
user47779 Avatar asked May 25 '26 03:05

user47779


1 Answers

The usual way to achieve the effect of variable templates in C++11 is to place them as static variable in a class template:

template<typename T, T V>
T my_value = V;

becomes

template<typename T, T V>
struct value_helper {
   static T value = V;
};

(I am bad at naming things, though.)

like image 173
j6t Avatar answered May 30 '26 09:05

j6t



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!