fork(1) download
  1. #include<string>
  2.  
  3. enum class type {
  4. integer, string
  5. };
  6.  
  7. template<type T>
  8. struct foo_impl {
  9. static constexpr bool is_valid = false;
  10. typedef void Type;
  11. };
  12.  
  13. template<>
  14. struct foo_impl<type::integer> {
  15. static constexpr bool is_valid = true;
  16. typedef int Type;
  17. };
  18.  
  19. template<>
  20. struct foo_impl<type::string> {
  21. static constexpr bool is_valid = true;
  22. typedef std::string Type;
  23. };
  24.  
  25. template<type T>
  26. struct foo {
  27. static_assert(foo_impl<T>::is_valid, "Not a valid type!");
  28.  
  29. using Type = typename foo_impl<T>::Type;
  30. Type value;
  31.  
  32. foo(Type v) : value(std::move(v)) {}
  33.  
  34. friend foo operator|(type const& lhs, foo const& rhs) {
  35. // do something
  36. return rhs;
  37. }
  38. };
  39.  
  40. template<type T>
  41. void test(foo<T>) {}
  42. int main() {
  43. using foo_int = foo<type::integer>;
  44. using foo_string = foo<type::string>;
  45.  
  46. // what I want (they currently all don't compile)
  47. test(type::integer | foo_int(10)); // ok
  48. //test(type::integer | foo_int(std::string("hello"))); // error
  49. test(type::string | foo_string(std::string("hello"))); // ok
  50. //test(type::string | foo_string(10)); // error
  51. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty