fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template<typename TypeHead, typename... TypeTail>
  5. struct TypesList
  6. {
  7. typedef TypeHead head;
  8. typedef TypesList<TypeTail...> tail;
  9.  
  10. enum { Length = 1 + tail::Length };
  11. static const bool isLast = false;
  12.  
  13. template<typename QueryType>
  14. struct Contains
  15. {
  16. static const bool value = std::is_same<QueryType, TypeHead>::value ||
  17. tail::template Contains<QueryType>::value;
  18. };
  19. };
  20.  
  21. template<typename TypeHead>
  22. struct TypesList<TypeHead>
  23. {
  24. typedef TypeHead head;
  25.  
  26. enum { Length = 1 };
  27. static const bool isLast = true;
  28.  
  29. template<typename QueryType>
  30. struct Contains
  31. {
  32. static const bool value = std::is_same<QueryType, TypeHead>::value;
  33. };
  34. };
  35.  
  36. template<typename T>
  37. void foo(const T &arg) {
  38. typedef TypesList<int, float, char> AllowedTypes;
  39.  
  40. static_assert(AllowedTypes::Contains<T>::value, "Not allowed type: ");
  41.  
  42. // do some stuff here
  43. }
  44.  
  45. struct Yoba {};
  46.  
  47. int main()
  48. {
  49. auto num = 5;
  50. foo(num);
  51.  
  52. Yoba yoba;
  53. foo(yoba);
  54.  
  55. return 0;
  56. }
  57.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void foo(const T&) [with T = Yoba]’:
prog.cpp:53:10:   required from here
prog.cpp:40:2: error: static assertion failed: Not allowed type: 
  static_assert(AllowedTypes::Contains<T>::value, "Not allowed type: ");
  ^
stdout
Standard output is empty