fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4.  
  5. template <bool Condition, class T = void>
  6. using enable_if = typename std::enable_if<Condition, T>::type;
  7.  
  8. template <class T>
  9. struct is_modifiable_rval
  10. : std::is_same<typename std::remove_const<typename std::remove_reference<T>::type>::type&&, T&&> {};
  11.  
  12.  
  13. template <class T>
  14. void fn(T const& x)
  15. {
  16. std::cout << "not a modifiable temporary" << std::endl;
  17. }
  18. template <class T>
  19. enable_if<is_modifiable_rval<T>{}> fn(T&& x)
  20. {
  21. std::cout << "a modifiable temporary" << std::endl;
  22. }
  23.  
  24.  
  25. template <class T>
  26. T expr()
  27. {
  28. static typename std::decay<T>::type t;
  29. return t;
  30. }
  31.  
  32. int main()
  33. {
  34. fn(expr<std::string&>());
  35. fn(expr<const std::string&>());
  36. fn(expr<std::string>());
  37. fn(expr<const std::string>());
  38. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
not a modifiable temporary
not a modifiable temporary
a modifiable temporary
not a modifiable temporary