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
3
votes
2 answers

Is it possible to pass variable template to function through lambda?

As I know how to pass template function as template argument, I'm now struggling to pass variable template in a similar manner. Here is minimal example of what I tried: #define PASS_VARIABLE_TEMPLATE(name) [dummy=nullptr](auto&&...args) \ …
xinaiz
  • 7,744
  • 6
  • 34
  • 78
3
votes
1 answer

Variable template fails to link

The following code: template constexpr T foo { 1.2345 }; template T fun(T x) { return -foo * x; } int main() { fun(2.0); } compiled using gcc version 5.1.0 on Linux g++ gcc-bug.cpp -std=c++14 fails during the…
Escualo
  • 40,844
  • 23
  • 87
  • 135
3
votes
1 answer

Simulating nan/inf in a constexpr variable template

Is there a way to simulate nan/inf in a constant expression and without! using the C macros HUGE_VAL and INFINITY or any other for that matter! Plus, even with them, it still isn't constexpr. I do not wish to use any standard function that the C++…
DeiDei
  • 10,205
  • 6
  • 55
  • 80
2
votes
3 answers

Define a global variable template?

Usually, we do declare but not define a global variable in the header file. However, we define templates in it. Then the issue arises: is it possible to define a global variable template? template uint8_t BitMask =…
2
votes
2 answers

C++ Type Traits if_v (automatic type deduction + ensure same type)

Consider the following code #include template constexpr T if_v = std::conditional_t, …
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
2
votes
0 answers

Float constant evaluates to 0 when utilizing variable template

In the following source, why does the DEG_TO_RAD_B constant evaluate to 0? /// @file Main.cpp // Includes #include #include // Constant definitions template const T PI = std::acos(-T(1)); const float…
2
votes
2 answers

Why does const affect linkage of an instantiation of a global variable template in gcc?

I'm taking addresses of the following instantiated variable templates from two translation units: template bool b = true; template const bool cb = true; template inline const bool icb = true; I'm printing addresses of…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
2
votes
0 answers

Visual Studio no template variables defined in type_traits

When I tried use std::extent_v template it was underscored with red and I got error "namespace std doesn't contain member extent_v" in IDE, but it compiles without an error ( is of course included). When I found its definition in…
aaalex88
  • 619
  • 4
  • 13
2
votes
1 answer

Spirit-X3 parsers stored in variable template specializations not working on Clang

I have a working Spirit-X3 parser, that can parse two closely related grammars for setting up draughts and checkers positions. I define two variable templates specializations as parsers for the two dialects of the grammar: // general variable…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
2
votes
1 answer

variable templates and std::enable_if

Can I use enable_if (or is there some alternative technique available) with template variables. e.g. typedef float Float; typedef double Double; template constexpr Bool IsFloat = std::is_same_v; template constexpr Bool…
user673679
  • 1,327
  • 1
  • 16
  • 35
2
votes
1 answer

How to introduce static_assert into template variable definition

How to introduce static_assert into template variable definition? My attemption is to use lambda function: #include #include #include namespace { #pragma clang diagnostic push #pragma clang diagnostic ignored…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
2
votes
2 answers

Variable template at class scope

Using N3651 as a basis, A variable template at class scope is a static data member template. The example given is: struct matrix_constants { template using pauli = hermitian_matrix; Yet all of the following…
user1508519
1
vote
0 answers

Recursive variable template specialization (and MSVC behaviour)

Given the code: template struct list{}; template list<> foo{}; template list>)> foo>{}; what is the type of foo>? I'm asking this…
rm -rf
  • 121
  • 4
1
vote
2 answers

Detecting if a generic lambda with certain arguments is invocable

I am experimenting with some lambda detection functionality. What I am trying to achieve is to be able to detect if I can call a lambda F with an argument of type const C& returning a result convertible to bool. The code below looks close to what I…
1
vote
2 answers

Why is there no variable template template parameter?

I'm planning to create a variable template that takes (variable) template-template parameter and one typename: template