fork download
  1. #include <type_traits>
  2.  
  3. template< typename T, T V, typename = void > struct integral_c ;
  4.  
  5. // a la std::integral_constant http://e...content-available-to-author-only...e.com/w/cpp/types/integral_constant
  6. template< typename T, T V >
  7. struct integral_c< T, V, typename std::enable_if< std::is_integral<T>::value >::type >
  8. {
  9. static constexpr T value = V ;
  10. typedef T value_type ;
  11. typedef integral_c type ;
  12. constexpr operator value_type() const { return value ; }
  13. };
  14.  
  15. template< int N > struct int_ : integral_c<int,N> {} ;
  16. template< long N > struct long_ : integral_c<long,N> {} ;
  17.  
  18. template < typename T, typename U > struct plus : integral_c<
  19. typename std::common_type< typename T::value_type, typename U::value_type >::type,
  20. T::value + U::value > {} ;
  21.  
  22. #include <iostream>
  23. #include <functional>
  24.  
  25. int main()
  26. {
  27. typedef int_<5> five ;
  28. typedef long_<7> seven ;
  29.  
  30. typedef plus<five,seven> twelve ;
  31. static_assert( std::is_same< twelve::value_type, long >::value, "error in common_type" ) ;
  32. std::cout << twelve::value << '\n' ; // 12
  33.  
  34. std::cout << std::minus<long>()( twelve(), five() ) << '\n' // 7
  35. << twelve() * seven() << '\n' ; // 84
  36. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
12
7
84