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
6
votes
1 answer

Splatting a struct

I have a bunch of structs like this with increasing number of members, but consistent member naming: struct one { int a; }; struct two { int a; int b; }; struct three { int a; int b; int c; }; I also have a templated function which I want to have…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
2 answers

Forwarding a non-type argument causes different behaviour on Variable Template

This seems to be another "who's doing it well?" question since gcc 6.0.0 and clang 3.7.0 behaves different. Let's suppose we have a variable template which takes a const char * as non template argument and is specialized for a given…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
5
votes
0 answers

Partial specilization of static variable template in class template

If I do partial specialization I got different results from clang and g++. template < typename T> class X { public: T i; X(T _i): i{_i}{} operator T(){ return i; } }; template < typename T2 > class Y { public: …
Klaus
  • 24,205
  • 7
  • 58
  • 113
5
votes
1 answer

C++ make type of variable depend on context?

I have the following code: // Case #1 float f = 1.0f; float f2 = sqrt(f * pi); // Case #2 double d = 1.0; double d2 = sqrt(d * pi); Is there any way to define the variable pi so that operator* and sqrt will operate on floats in Case #1, but will…
Bernard
  • 5,209
  • 1
  • 34
  • 64
5
votes
1 answer

Specialization of template variable (for template template class)

When I try specialize a template variable for a generic container (e.g. std::list<...>, and not for an specific one, e.g. std::list) I get a link error with gcc 5.3 (but not with clang 3.5) /tmp/ccvxFv3R.s: Assembler…
alfC
  • 14,261
  • 4
  • 67
  • 118
5
votes
1 answer

Can type to value meta-function be used as variable alias in C++14?

While looking at C++14 the meta-function alias proposals (TransformationTraits Redux, v2,N3655), I noticed that, not only type to type transformations (such as add_const), type to value meta-functions (such as is_void ) are also type aliased. (Which…
abir
  • 1,797
  • 14
  • 26
4
votes
0 answers

Variable templates with incomplete type

Suppose we have the following code: struct foo; struct bar { static foo mem; template static foo tpl_mem; }; using mem_t = decltype(bar::mem); // (1) using tpl_mem_t = decltype(bar::tpl_mem); // (2) It is my…
rm -rf
  • 121
  • 4
4
votes
0 answers

Explicit instantiation of a variable template defined with `auto` keyword

If one has a variable template with the type deduced from the initializer by means of auto keyword, for example: template auto * p = (T*)nullptr; How to make an instantiation of the variable for a particular type (e.g. to export it…
Fedor
  • 17,146
  • 13
  • 40
  • 131
4
votes
1 answer

How does template affect linkage of const global variable?

As the doc says (emphasis mine): Any of the following names declared at namespace scope have internal linkage: non-volatile non-template non-inline const-qualified variables (including constexpr) that aren't declared extern and aren't previously…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
4
votes
2 answers

Using a variable template inside inline constexpr function without exposing the variable template?

Is it possible to use a variable template inside an inline constexpr function without also exposing the variable template itself? For example, this compiles and works: template constexpr T twelve_hundred = T(1200.0); template
Danra
  • 9,546
  • 5
  • 59
  • 117
4
votes
2 answers

Are template variables thread safe? they're placed on data segment?

I'm playing with the new template variables feature from C++14 in order to get used to it (maybe is soon to do this 'cause it seems that some compilers didn't implement it completely). Now I'm wondering where lies each instance of a template…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
4
votes
1 answer

Recursive variable templates

I was trying to use variable templates the same way I use some other templates, for example: we already know how to calculate a Fibonacci number or the power of a number using metaprogramming with template objects wrapping a static value or a enum…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
4
votes
1 answer

Clang auto variable template bug

I went to see if you could use auto in a variable template declaration. template auto F = T{}; Fine, but as soon as you try to use it, clang craps. int f = F; // error: cannot initialize a variable of type 'int' with an lvalue of…
Tory Webster
  • 365
  • 2
  • 6
3
votes
1 answer

How do I expand integer_sequence?

I have a function which looks like this: template std::ostream& vector_insert(std::ostream& lhs, const char* delim, const T& rhs, std::index_sequence) { std::ostream_iterator it(lhs, delim); …
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
2 answers

Division in Variable Template Returns Zero in Visual Studio 2017

This is probably a visual-studio-2017 bug related to this question: Templated Variables Bug With Lambdas in Visual Studio? And as mentioned in the comments there seems to be optimizer related. Division in the definition of a variable template seems…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288