Mar 28, 2014

[C++14] Fun with variable templates

C++14 variable template with default template parameter
template <typename T = double> constexpr T pi = T(3.1415926535897932385);
pi<>;
C++1y/C++14: Variable Template Specialization
template<typename T>
int a = T(); // --1

template<>
int a<int> = int(); // --2

a<int>; // will call (2)
#include <iostream>
#include <typeinfo>
#include <string>

using namespace std;

struct LifeCreator
{
   void Vivid()
   {
      cout << "Hello there!" << endl;
   }

   LifeCreator GiveMeR()
   {
      return LifeCreator();
   }

   void Yell()
   {
      cout << "This Is Fun!" << endl;
   }
};


template<typename T>
struct Talk
{
   void operator = (T words)
   {
      cout << words << endl;
   }
};

template<typename T>
T MeaningOfLife = &LifeCreator::Vivid;

template<typename T>
T&& RValueWorld = T().GiveMeR();

template<typename T>
T Translator = T();

int main()
{
   (static_cast<LifeCreator*>(0)->*MeaningOfLife<void (LifeCreator::*)()>)();
   RValueWorld<LifeCreator>.Yell();
   Translator<Talk<string>> = "This", Translator<Talk<string>> = "is" ,
   Translator<Talk<string>> = "so", Translator<Talk<string>> = "Auh~~";
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.