fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. struct A { };
  5. struct B { };
  6.  
  7. struct C {
  8. template <typename U,
  9. typename std::enable_if<std::is_integral<U>::value>::type* = nullptr>
  10. explicit operator U() const {
  11. return 1;
  12. }
  13.  
  14. template<typename U,
  15. typename std::enable_if<std::is_same<U, A>::value>::type* = nullptr>
  16. explicit operator U() const {
  17. return A{ };
  18. }
  19.  
  20. template<typename U,
  21. typename std::enable_if<std::is_same<U, B>::value>::type* = nullptr>
  22. explicit operator U() const {
  23. return B{ };
  24. }
  25.  
  26. };
  27.  
  28. int main() {
  29. C c;
  30. long y = static_cast<int>(c);
  31. B b = static_cast<B>(c);
  32. A a = static_cast<A>(c);
  33. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty