fork download
  1. // VC2015Test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. //#include "stdafx.h"
  5.  
  6. #include <array>
  7. #include <functional>
  8. #include <array>
  9. #include <functional>
  10.  
  11. template <typename... T>
  12. using common_type_t = typename std::common_type<T...>::type;
  13.  
  14. template <typename T>
  15. using remove_cv_t = typename std::remove_cv<T>::type;
  16.  
  17. template <bool, typename T, typename... U>
  18. struct lazy_conditional_c;
  19.  
  20. template <typename T>
  21. struct lazy_conditional_c<true, T>
  22. {
  23. using type = typename T::type;
  24. };
  25.  
  26. template <typename T, typename U>
  27. struct lazy_conditional_c<true, T, U>
  28. {
  29. using type = typename T::type;
  30. };
  31.  
  32. template <typename T, typename U>
  33. struct lazy_conditional_c<false, T, U>
  34. {
  35. using type = typename U::type;
  36. };
  37.  
  38. template <typename V, typename T, typename... U>
  39. using if_else = lazy_conditional_c<V::value, T, U...>;
  40.  
  41. template <typename V, typename T, typename... U>
  42. using If = typename if_else<V, T, U...>::type;
  43.  
  44. template <typename T>
  45. struct identity_of
  46. {
  47. using type = T;
  48. };
  49.  
  50. template <template <typename> class F, typename... T>
  51. struct no_type : std::true_type {};
  52.  
  53. template <template <typename> class F, typename T1, typename... T2>
  54. struct no_type<F, T1, T2...> :
  55. std::integral_constant
  56. <
  57. bool,
  58. !F<T1>::value && no_type<F, T2...>::value
  59. >
  60. {};
  61.  
  62. template <template <typename> class F, template <typename> class G>
  63. struct composed
  64. {
  65. template <typename T>
  66. using call = F<typename G<T>::type>;
  67. };
  68.  
  69. template <typename T>
  70. struct _is_reference_wrapper : std::false_type {};
  71.  
  72. template <typename T>
  73. struct _is_reference_wrapper<std::reference_wrapper<T>> : std::true_type{};
  74.  
  75. template <typename T>
  76. using is_reference_wrapper =
  77. composed<_is_reference_wrapper, std::remove_cv>::call<T>;
  78.  
  79. template <typename V = void, typename... T>
  80. constexpr auto make_array(T&&... t)
  81. ->std::array
  82. <
  83. If
  84. <
  85. std::is_void<V>,
  86. std::common_type<T...>,
  87. identity_of<V>
  88. >,
  89. sizeof...(T)
  90. >
  91. {
  92. static_assert(no_type
  93. <
  94. composed
  95. <
  96. is_reference_wrapper,
  97. std::decay
  98. >
  99. ::call,
  100. T...
  101. >(), "T shall not be reference_wrapper");
  102.  
  103. return{ { std::forward<T>(t)... } };
  104. }
  105.  
  106. template <size_t... I>
  107. struct _indices {};
  108.  
  109. template <size_t N, size_t... I>
  110. struct _build_indices : _build_indices<N - 1, N - 1, I...> {};
  111.  
  112. template <size_t... I>
  113. struct _build_indices<0, I...> : _indices<I...>{};
  114.  
  115. template <typename T, size_t N, size_t... I>
  116. constexpr auto _to_array(T(&arr)[N], _indices<I...>)
  117. ->std::array<remove_cv_t<T>, N>
  118. {
  119. return{ { arr[I]... } };
  120. }
  121.  
  122. template <typename T, size_t N>
  123. constexpr auto to_array(T(&arr)[N])
  124. ->std::array<remove_cv_t<T>, N>
  125. {
  126. return _to_array(arr, _build_indices<N>());
  127. }
  128.  
  129.  
  130. #include <iostream>
  131.  
  132. int main(int argc, char* argv[])
  133. {
  134. auto ch = 'a';
  135. auto const d = 65l;
  136. auto a1 = make_array(ch, d, 0);
  137. static_assert(std::is_same<decltype(a1)::value_type, long>(), "");
  138. std::cout << a1[0] << std::endl;
  139.  
  140. constexpr auto a2 = to_array("abc");
  141. static_assert(std::is_same<decltype(a2)::value_type, char>(), "");
  142. std::cout << a2.data() << std::endl;
  143.  
  144. auto a3 = make_array("aa", "bb");
  145. static_assert(std::is_same<decltype(a3)::value_type, char const*>(),
  146. "fix your library");
  147. std::cout << a3[0] << std::endl;
  148.  
  149. auto a4 = make_array<long>(2, 3U);
  150. static_assert(std::is_same<decltype(a4)::value_type, long>(), "");
  151. std::cout << a4[0] << std::endl;
  152.  
  153. auto a5 = make_array<short>();
  154. static_assert(std::is_same<decltype(a5)::value_type, short>(), "");
  155. std::cout << a5.size() << std::endl;
  156.  
  157. int a, b;
  158. auto a6 = make_array<std::reference_wrapper<int>>(a, b);
  159. std::cout << a6.size() << std::endl;
  160.  
  161. // ** do not compile **
  162. //auto a7 = make_array(std::cref(""));
  163.  
  164. // ** hard error **
  165. //char s[2][6] = { "nice", "thing" };
  166. //auto a8 = to_array(s);
  167. }
  168.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/4.9/array:35:0,
                 from prog.cpp:6:
/usr/include/c++/4.9/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
prog.cpp:11:19: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <typename... T>
                   ^
prog.cpp:12:1: error: expected unqualified-id before 'using'
 using common_type_t = typename std::common_type<T...>::type;
 ^
prog.cpp:15:1: error: expected unqualified-id before 'using'
 using remove_cv_t = typename std::remove_cv<T>::type;
 ^
prog.cpp:17:37: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <bool, typename T, typename... U>
                                     ^
prog.cpp:23:9: error: expected nested-name-specifier before 'type'
   using type = typename T::type;
         ^
prog.cpp:29:9: error: expected nested-name-specifier before 'type'
   using type = typename T::type;
         ^
prog.cpp:35:9: error: expected nested-name-specifier before 'type'
   using type = typename U::type;
         ^
prog.cpp:38:43: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <typename V, typename T, typename... U>
                                           ^
prog.cpp:39:1: error: expected unqualified-id before 'using'
 using if_else = lazy_conditional_c<V::value, T, U...>;
 ^
prog.cpp:41:43: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <typename V, typename T, typename... U>
                                           ^
prog.cpp:42:1: error: expected unqualified-id before 'using'
 using If = typename if_else<V, T, U...>::type;
 ^
prog.cpp:47:9: error: expected nested-name-specifier before 'type'
   using type = T;
         ^
prog.cpp:50:48: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <template <typename> class F, typename... T>
                                                ^
prog.cpp:51:33: error: expected class-name before '{' token
 struct no_type : std::true_type {};
                                 ^
prog.cpp:53:61: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <template <typename> class F, typename T1, typename... T2>
                                                             ^
prog.cpp:56:3: error: expected template-name before '<' token
   <
   ^
prog.cpp:56:3: error: expected '{' before '<' token
prog.cpp:56:3: error: expected unqualified-id before '<' token
prog.cpp:66:3: error: expected unqualified-id before 'using'
   using call = F<typename G<T>::type>;
   ^
prog.cpp:70:48: error: expected class-name before '{' token
 struct _is_reference_wrapper : std::false_type {};
                                                ^
prog.cpp:73:30: error: 'reference_wrapper' is not a member of 'std'
 struct _is_reference_wrapper<std::reference_wrapper<T>> : std::true_type{};
                              ^
prog.cpp:73:30: error: 'reference_wrapper' is not a member of 'std'
prog.cpp:73:64: error: template argument 1 is invalid
 struct _is_reference_wrapper<std::reference_wrapper<T>> : std::true_type{};
                                                                ^
prog.cpp:76:1: error: expected unqualified-id before 'using'
 using is_reference_wrapper =
 ^
prog.cpp:79:38: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <typename V = void, typename... T>
                                      ^
prog.cpp:80:1: error: 'constexpr' does not name a type
 constexpr auto make_array(T&&... t)
 ^
prog.cpp:80:1: note: C++11 'constexpr' only available with -std=c++11 or -std=gnu++11
prog.cpp:106:11: error: 'size_t' has not been declared
 template <size_t... I>
           ^
prog.cpp:106:21: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <size_t... I>
                     ^
prog.cpp:109:11: error: 'size_t' has not been declared
 template <size_t N, size_t... I>
           ^
prog.cpp:109:21: error: 'size_t' has not been declared
 template <size_t N, size_t... I>
                     ^
prog.cpp:109:31: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <size_t N, size_t... I>
                               ^
prog.cpp:112:11: error: 'size_t' has not been declared
 template <size_t... I>
           ^
prog.cpp:112:21: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <size_t... I>
                     ^
prog.cpp:115:23: error: 'size_t' has not been declared
 template <typename T, size_t N, size_t... I>
                       ^
prog.cpp:115:33: error: 'size_t' has not been declared
 template <typename T, size_t N, size_t... I>
                                 ^
prog.cpp:115:43: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <typename T, size_t N, size_t... I>
                                           ^
prog.cpp:116:1: error: 'constexpr' does not name a type
 constexpr auto _to_array(T(&arr)[N], _indices<I...>)
 ^
prog.cpp:116:1: note: C++11 'constexpr' only available with -std=c++11 or -std=gnu++11
prog.cpp:122:23: error: 'size_t' has not been declared
 template <typename T, size_t N>
                       ^
prog.cpp:123:1: error: 'constexpr' does not name a type
 constexpr auto to_array(T(&arr)[N])
 ^
prog.cpp:123:1: note: C++11 'constexpr' only available with -std=c++11 or -std=gnu++11
prog.cpp: In function 'int main(int, char**)':
prog.cpp:134:8: error: 'ch' does not name a type
   auto ch = 'a';
        ^
prog.cpp:135:14: error: 'd' does not name a type
   auto const d = 65l;
              ^
prog.cpp:136:8: error: 'a1' does not name a type
   auto a1 = make_array(ch, d, 0);
        ^
prog.cpp:137:17: error: 'is_same' is not a member of 'std'
   static_assert(std::is_same<decltype(a1)::value_type, long>(), "");
                 ^
prog.cpp:137:39: error: 'a1' was not declared in this scope
   static_assert(std::is_same<decltype(a1)::value_type, long>(), "");
                                       ^
prog.cpp:137:41: error: 'decltype' was not declared in this scope
   static_assert(std::is_same<decltype(a1)::value_type, long>(), "");
                                         ^
prog.cpp:137:56: error: expected primary-expression before 'long'
   static_assert(std::is_same<decltype(a1)::value_type, long>(), "");
                                                        ^
prog.cpp:137:67: error: 'static_assert' was not declared in this scope
   static_assert(std::is_same<decltype(a1)::value_type, long>(), "");
                                                                   ^
prog.cpp:140:3: error: 'constexpr' was not declared in this scope
   constexpr auto a2 = to_array("abc");
   ^
prog.cpp:141:17: error: 'is_same' is not a member of 'std'
   static_assert(std::is_same<decltype(a2)::value_type, char>(), "");
                 ^
prog.cpp:141:39: error: 'a2' was not declared in this scope
   static_assert(std::is_same<decltype(a2)::value_type, char>(), "");
                                       ^
prog.cpp:141:56: error: expected primary-expression before 'char'
   static_assert(std::is_same<decltype(a2)::value_type, char>(), "");
                                                        ^
prog.cpp:144:8: error: 'a3' does not name a type
   auto a3 = make_array("aa", "bb");
        ^
prog.cpp:145:17: error: 'is_same' is not a member of 'std'
   static_assert(std::is_same<decltype(a3)::value_type, char const*>(),
                 ^
prog.cpp:145:39: error: 'a3' was not declared in this scope
   static_assert(std::is_same<decltype(a3)::value_type, char const*>(),
                                       ^
prog.cpp:145:56: error: expected primary-expression before 'char'
   static_assert(std::is_same<decltype(a3)::value_type, char const*>(),
                                                        ^
prog.cpp:149:8: error: 'a4' does not name a type
   auto a4 = make_array<long>(2, 3U);
        ^
prog.cpp:150:17: error: 'is_same' is not a member of 'std'
   static_assert(std::is_same<decltype(a4)::value_type, long>(), "");
                 ^
prog.cpp:150:39: error: 'a4' was not declared in this scope
   static_assert(std::is_same<decltype(a4)::value_type, long>(), "");
                                       ^
prog.cpp:150:56: error: expected primary-expression before 'long'
   static_assert(std::is_same<decltype(a4)::value_type, long>(), "");
                                                        ^
prog.cpp:153:8: error: 'a5' does not name a type
   auto a5 = make_array<short>();
        ^
prog.cpp:154:17: error: 'is_same' is not a member of 'std'
   static_assert(std::is_same<decltype(a5)::value_type, short>(), "");
                 ^
prog.cpp:154:39: error: 'a5' was not declared in this scope
   static_assert(std::is_same<decltype(a5)::value_type, short>(), "");
                                       ^
prog.cpp:154:56: error: expected primary-expression before 'short'
   static_assert(std::is_same<decltype(a5)::value_type, short>(), "");
                                                        ^
prog.cpp:158:8: error: 'a6' does not name a type
   auto a6 = make_array<std::reference_wrapper<int>>(a, b);
        ^
prog.cpp:159:16: error: 'a6' was not declared in this scope
   std::cout << a6.size() << std::endl;
                ^
stdout
Standard output is empty