Saturday, October 15, 2016

type traits is_const and variadic template extending std::min

Eclipse CDT tool chain has an issue--"using std::is_heap" not declared in  
so used VS 2015 instead.

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <type_traits>

using namespace std;

template<typename T>
struct is_const2
{
 static const bool value = false;
};

template<typename T>
struct is_const2<const T>  // specialization
{
 static const bool value = true;
};

template<typename T>
inline T min2(const T& t) { return t; }

template<typename T, typename ...P>
inline auto min2(const T& t, const P& ...p)
{
 using res_type = std::common_type_t<T, P...>;
 return std::min(res_type(t), res_type(min2(p...)));
};

int main() {

 cout << "test " << is_const2<const int*>::value << endl;
 cout << "test " << std::is_const<int* const>::value << endl;
 cout << "test " << is_const2<const int&>::value << endl;

 const int i = 123;
 const double j = 456.1;
 const char k = 'a';

 auto m = min2(i,j,k);

 return 0;
}


No comments:

Post a Comment