fork download
  1. #include <initializer_list>
  2. #include <iostream>
  3. #include <string>
  4. #include <type_traits>
  5.  
  6. using namespace std;
  7.  
  8. void fun (const std::initializer_list<std::string>& strings) {
  9. for(auto s : strings)
  10. cout << s << endl;
  11. }
  12.  
  13. template <typename T>
  14. struct HasValue
  15. {
  16. typedef char OK;
  17. struct BAD { char x[2]; };
  18. template <const string *>
  19. struct Helper;
  20. template <typename X>
  21. static OK has(X*, Helper<&X::value>* = nullptr);
  22. static BAD has(...);
  23.  
  24. static const bool value = (sizeof(has((T*)nullptr)) == sizeof(OK));
  25. };
  26.  
  27. template <typename H, typename... T>
  28. struct HaveValue : public integral_constant<bool, HasValue<H>::value && HaveValue<T...>::value>
  29. {};
  30.  
  31. template <typename H>
  32. struct HaveValue<H> : public HasValue<H>
  33. {};
  34.  
  35.  
  36.  
  37. template <typename... Args>
  38. void foo() {
  39. static_assert(HaveValue<Args...>::value, "All arguments must have const string ::value");
  40. fun({Args::value...});
  41. }
  42.  
  43. struct A
  44. {
  45. static const string value;
  46. };
  47. const string A::value = "AA";
  48.  
  49. struct B
  50. {
  51. static const string value;
  52. };
  53. const string B::value = "BB";
  54.  
  55. struct C{};
  56.  
  57. int main()
  58. {
  59. foo<A, B>();
  60. //foo<A, B, C>();
  61. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
AA
BB