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


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

Lambda auto in parameters


File: lam1.cpp
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

using namespace std ;


int main()
{
   //C++ 14 feature auto for argument types
   auto identity = [](auto x1) { return x1; };
   int three = identity(3); // == 3
   string foo = identity("foo"); // == "foo"

    return 1 ;
}

Using auto as the return type from a function


File: auto2.cpp
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

using namespace std ;

//C++ 14 Deduce return type as `int`.
auto f1(int i1)
{
 return i1;
}



int main()
{
   int result = f1( 3 ) ;

    return 1 ;
}