fork(2) download
  1. #include <iostream>
  2.  
  3. typedef std::size_t SizeType;
  4.  
  5. template<typename T>
  6. struct Identity
  7. {
  8. typedef T Type;
  9. };
  10.  
  11. typedef Identity<char(&)[1]>::Type NoType;
  12. typedef Identity<char(&)[2]>::Type YesType;
  13.  
  14. template<typename T, T Val>
  15. struct IntegralConstant : public Identity<IntegralConstant<T, Val> >
  16. {
  17. typedef T ValueType;
  18.  
  19. #ifdef Lib_Cpp11
  20. static constexpr ValueType Value = Val;
  21. constexpr operator ValueType() const
  22. {
  23. return this->Value;
  24. }
  25. #else // Lib_Cpp11
  26. static const ValueType Value = Val;
  27. operator ValueType() const
  28. {
  29. return this->Value;
  30. }
  31. #endif // Lib_Cpp11
  32. };
  33.  
  34. template<SizeType Val>
  35. struct SizeToType : public IntegralConstant<SizeType, Val>
  36. {
  37. };
  38.  
  39. template<bool Val>
  40. struct BoolToType : public IntegralConstant<bool, Val>
  41. {
  42. };
  43.  
  44. template<typename T>
  45. T& DeclVal();
  46.  
  47. namespace Detail
  48. {
  49. template<typename From, typename To>
  50. class IsExplicitConvertible
  51. {
  52. template<typename FromT, typename ToT>
  53. static NoType Check(...);
  54. template<typename FromT, typename ToT>
  55. static YesType Check(SizeToType<sizeof(static_cast<ToT>(DeclVal<FromT>()))>*);
  56.  
  57. public:
  58. static const bool Value = sizeof(Check<From, To>(0)) == sizeof(YesType);
  59. };
  60.  
  61. template<typename T>
  62. class IsDefaultConstructible
  63. {
  64. template<typename U>
  65. static NoType Check(...);
  66. template<typename U>
  67. static YesType Check(SizeToType<sizeof(U())>*);
  68.  
  69. public:
  70. static const bool Value = sizeof(Check<T>(0)) == sizeof(YesType);
  71. };
  72. }
  73.  
  74. template<typename From, typename To>
  75. struct IsExplicitConvertible : public BoolToType<Detail::IsExplicitConvertible<From, To>::Value>
  76. {
  77. };
  78.  
  79. template<typename T>
  80. struct IsDefaultConstructible : public BoolToType<Detail::IsDefaultConstructible<T>::Value>
  81. {
  82. };
  83.  
  84. struct Foo1
  85. {
  86. explicit Foo1(int);
  87.  
  88. operator char();
  89. };
  90.  
  91. class Foo2
  92. {
  93. Foo2();
  94. };
  95.  
  96. struct Foo3
  97. {
  98. };
  99.  
  100. int main()
  101. {
  102. std::cout << IsExplicitConvertible<int, float>::Value << '\n'; // Sollte 1 sein
  103. std::cout << IsExplicitConvertible<int, Foo1>::Value << '\n'; // Sollte 1 sein
  104. std::cout << IsExplicitConvertible<float, Foo1>::Value << '\n'; // Sollte 1 sein
  105. std::cout << IsExplicitConvertible<Foo1, char>::Value << '\n'; // Sollte 1 sein
  106. std::cout << IsExplicitConvertible<Foo1, double>::Value << '\n'; // Sollte 1 sein
  107. std::cout << IsExplicitConvertible<bool, Foo2>::Value << '\n'; // Sollte 0 sein
  108. std::cout << IsExplicitConvertible<int, void*>::Value << '\n'; // Sollte 0 sein
  109. std::cout << '\n';
  110. std::cout << IsDefaultConstructible<int>::Value << '\n'; // Sollte 1 sein
  111. std::cout << IsDefaultConstructible<Foo1>::Value << '\n'; // Sollte 0 sein
  112. std::cout << IsDefaultConstructible<Foo2>::Value << '\n'; // Sollte 0 sein
  113. std::cout << IsDefaultConstructible<Foo3>::Value << '\n'; // Sollte 1 sein
  114. }
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
1
1
1
1
1
0
0

0
0
0
0