Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

May variable template variables vary?

Draft C++14 has added support for variable templates. The examples in the proposal (N3651) all show constants (either constexpr or const), but, from what I can tell, this is not required. May variable template variables vary, e.g., be assigned to?

template<typename T>
T magicVal = 42;

magicVal<int> = 0;       // okay?
like image 711
KnowItAllWannabe Avatar asked Mar 26 '26 23:03

KnowItAllWannabe


1 Answers

Yes, variable template instances are first-class objects. Effectively a template-id is just a name.

"Under the hood" a variable template just the same as an old-fashioned class template with a single static member.

Edit: It seems that the proposal was written in terms of constexpr constants only, such as to generically represent mathematical entities. Extension to other objects was requested by the reviewing committee and added as an afterthought.

like image 197
Potatoswatter Avatar answered Mar 28 '26 14:03

Potatoswatter



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!