fork(5) download
  1. #include <type_traits>
  2.  
  3. template <typename T, typename... Ts> struct contains;
  4.  
  5. template <typename T> struct contains<T> : std::false_type {};
  6.  
  7. template <typename T, typename... Ts>
  8. struct contains<T, T, Ts...> : std::true_type {};
  9.  
  10. template <typename T, typename T2, typename... Ts>
  11. struct contains<T, T2, Ts...> : contains<T, Ts...> {};
  12.  
  13. static_assert(contains<int, float, char, int, void*>::value, "");
  14. static_assert(!contains<int, float*, char*, int*, void*>::value, "");
  15.  
  16. template <typename T, typename... Ts> struct get_index;
  17.  
  18. template <typename T, typename... Ts>
  19. struct get_index<T, T, Ts...> : std::integral_constant<std::size_t, 0>
  20. {
  21. static_assert(!contains<T, Ts...>::value, "Duplicate T");
  22. };
  23.  
  24. template <typename T, typename Tail, typename... Ts>
  25. struct get_index<T, Tail, Ts...> :
  26. std::integral_constant<std::size_t, 1 + get_index<T, Ts...>::value> {};
  27.  
  28. #if 1 // explicit error case, but you already have error without that.
  29. template <typename T>
  30. struct get_index<T>
  31. {
  32. // condition is always false, but should be dependant of T
  33. static_assert(sizeof(T) == 0, "element not found");
  34. };
  35. #endif
  36.  
  37. template struct get_index<int, char, int, void>; // ok
  38. template struct get_index<int, char, int, void, int>; // duplicated int
  39.  
  40. int main() {
  41. }
Compilation error #stdin compilation error #stdout 0s 3456KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'struct get_index<int, int, void, int>':
prog.cpp:26:43:   required from 'struct get_index<int, char, int, void, int>'
prog.cpp:38:17:   required from here
prog.cpp:21:2: error: static assertion failed: Duplicate T
  static_assert(!contains<T, Ts...>::value, "Duplicate T");
  ^
stdout
Standard output is empty