Questions tagged [variable-templates]

Use this tag for anything related to C++14 variable templates.

Variable templates are a feature of C++ as of C++14. They allow variables to be declared as templates, just like functions and classes:

template<class T>
constexpr T pi = T(3.1415926535897932385);

template<class T>
    T circular_area(T r) {
    return pi<T> * r * r;
}
68 questions
19
votes
2 answers

Variable template in template class - unexpected error (possible bug?)

Having: struct Value { template static constexpr T value{0}; }; (0) ideone template struct Something { void x() { static_assert(TValue::template value == 0, ""); } }; int main() {…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
19
votes
1 answer

C++14 warning: too many template headers for variable (should be 0)

While experimenting with the recent g++-5 compiler, I wrote below statement in a file: template T a; template<> int a = 1; Which results in: warning: too many template headers for a (should be 0) Also effectively, it doesn't really specialize…
iammilind
  • 68,093
  • 33
  • 169
  • 336
17
votes
1 answer

How should a Variable Template be referred to in C++14 when declared at Class scope?

For example: class example{ public: template static constexpr T var = T(1.5); }; int main(){ int a = example::var; example obj; int b = obj.var; return 0; } GCC produces error for both:…
José Luis
  • 397
  • 1
  • 11
15
votes
1 answer

Can a variable template be passed as a template template argument?

The following nonsensical example does not compile, but is there some other way to pass a variable template as a template template argument? template constexpr auto zero = T{0}; template auto…
invexed
  • 645
  • 3
  • 10
14
votes
1 answer

Recursive computation using variable templates - gcc vs clang

Consider the following example: #include template int fib = fib + fib; template <> int fib<2> = 1; template <> int fib<1> = 1; int main() { std::printf("%d %d %d", fib<4>, fib<5>, fib<6>); } GCC 7.x, 8.x, 9.x,…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
13
votes
1 answer

Is initialization of a reference variable primary template required even when it is never instantiated?

Is it legal to declare a reference template in C++14 without initializing the primary reference template, as long as it is never instantiated? template const T& ref; template<> auto ref = 1; auto x = ref; This produces…
leek
  • 861
  • 8
  • 12
12
votes
2 answers

Forward declare a constexpr variable template

I tried to forward-declare a constexpr variable template like this: template constexpr std::size_t iterator_category_value; The goal was to document that every specialization should be constexpr but I have to admit that I never checked…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
9
votes
1 answer

Partial Specialization of Variable Templates

I know that I can partially specialize class templates, and I know that I cannot partially specify function templates. What about variable templates? I can't find documentation on whether they can be partially specialized.
9
votes
1 answer

Templated Variables Bug With Lambdas in Visual Studio?

c++14 provides variable templates Which work fine in visual-studio-2017, but within lambdas they seem to fall apart. For example: template const auto PI = std::acos(static_cast(-1)); int main() { auto func = []() { cout <<…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
9
votes
2 answers

Variable template template?

Say you have a tuple type and you want to extract its template parameter pack in order to instantiate another template. If that is a type template, then I can have a utility like this: template < typename Tuple, template typename What…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
8
votes
2 answers

Type_traits *_v variable template utility order fails to compile

Having seen this answer, I tried to come up with a variable template utility to the code from it: template class Template> struct is_specialization : std::false_type {}; template