fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template <int I, class T>
  5. using enableIf = typename std::enable_if<I, T>::type;
  6.  
  7. template <class T>
  8. struct optional{
  9. template <class U>
  10. enableIf<std::is_convertible<U, T>::value, T> value_or(U&& v) const{
  11. std::cout << "value_or(U&& v)\n";
  12. return std::move(v);
  13. }
  14.  
  15. template <class F>
  16. auto value_or(F&& action) const -> decltype(action()) {
  17. return action();
  18. }
  19. };
  20.  
  21. int main() {
  22. optional<int> o1;
  23. o1.value_or(1);
  24. int i;
  25. o1.value_or(i);
  26. o1.value_or([](){ std::cout<< "lambda\n"; return 0; });
  27. return 0;
  28. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
value_or(U&& v)
value_or(U&& v)
lambda