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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With