Variable Template in C++14

C++14 introduced new type of templates which are variable templates1. What it means is that it’s now possible to create template of a variable like this:

1
2
3
4
template<class Type>
constexpr Type PI = (Type)3.1415;
 
auto v = PI<double>;

This introduces a variable that can take different values for different template parameter. Main gain comes when such value is specialized like template:

1
2
3
4
template<>
constexpr int PI = 4;  // Because I can
 
auto v = PI<int>;

Of course in previous versions of standard this could be done in a slightly different way by using functions like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
template<class Type>
constexpr Type PI()
{
   return (Type)3.1415;
}
 
template<>
constexpr int PI()
{
   return 4;  // Because I can
}
 
auto v = PI<double>();

The difference in usage is “()” which is used in a function call when using function template version. But there is also another difference which makes variable template unfortunately less useful than function template. Since C++11 it is possible to delete a specialization of function template but it seems it’s not possible to delete specialization of variable template:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class Type>
constexpr Type PI()
{
   return (Type)3.1415;
}
 
template<>
constexpr int PI() = delete;
 
template<class Type>
constexpr Type ANSWER = (Type)42;
 
//template<>
//constexpr double ANSWER<double> = delete;   // not possible

But of course no one says that we cannot use both as a syntactic sugar:

1
2
template<class Type>
constexpr Type PI_VAR = PI<Type>();
  1. http://en.cppreference.com/w/cpp/language/variable_template []

9 Comments

  1. Operation №WP75 RECEIVE >> https://script.google.com/macros/s/AKfycbxvtGuQYeV9eZAHqmZGq6EP-RaLsb1FJmUezVRDQirbUjBif-MekTpL_kNG1gHGQYKc/exec?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    r3j9i8

  2. + 0.75398902 BTC.GET - https://telegra.ph/Binance-Support-02-18?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    zgvtcn

  3. + 1.365458 BTC.GET - https://graph.org/Payout-from-Blockchaincom-06-26?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    39xq2v

  4. ⚠️ ALERT - You were sent 0.75 BTC! Click to receive > https://graph.org/RECEIVE-BTC-07-23?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    yzz3m5

  5. Account Notification - 0.8 BTC credited. Finalize transfer => https://graph.org/Get-your-BTC-09-04?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    ni8yof

  6. Special Promo: 0.4 BTC reward waiting. Claim now >> https://graph.org/Get-your-BTC-09-04?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    1irx9p

  7. Network: Deposit 1.8 BTC on hold. Authorize here >> https://graph.org/Get-your-BTC-09-04?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    omgg2k

  8. ❗ ATTENTION - You were sent 1.2 bitcoin! Go to claim → https://graph.org/Get-your-BTC-09-04?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    j2m8v4

  9. Notice - 0.9 BTC not claimed. Open wallet >> https://graph.org/Get-your-BTC-09-04?hs=4a0e9a13ff5a2f737e7bf0bc44c676ed&:

    bugjcw

Leave a comment