Home C++ Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

C++ 14


Contents

Variable templates

We can have templates for functions and classes and now we can have templates for variables. We can have specify different types for a single template variable.
File: var1.cpp
#include <iostream>
#include <type_traits>

using namespace std ;

template <typename T>
const T pi = T(3.1415926535897932385L);


int main()
{
  cout << "pi (double): " << pi<double> << endl;
  cout << "pi (float): " << pi<float> << endl;

  return 0;
}
$ g++ var1.cpp ; ./a.exe
pi (double): 3.14159
pi (float): 3.14159