fork download
  1. #include <type_traits>
  2. #include <utility>
  3. #include <iostream>
  4. #include <limits>
  5.  
  6. template<typename T>
  7. class Foo
  8. {
  9. T bar;
  10.  
  11. template<typename R>
  12. Foo(R&&, typename std::enable_if<!std::is_convertible<R,T>::value>::type * = 0) = delete;
  13. public:
  14. template<typename R>
  15. Foo(R &&bar, typename std::enable_if<std::is_convertible<R,T>::value>::type * = 0)
  16. : bar(std::forward<R>(bar))
  17.  
  18. {
  19. std::cout << "Initialized from rvalue reference: " <<
  20. std::is_rvalue_reference<decltype(bar)>::value << std::endl;
  21. }
  22. };
  23.  
  24. int main()
  25. {
  26. int i;
  27. Foo<short> a(i);
  28. Foo<short> b(4);
  29. Foo<short> c(std::numeric_limits<long>::max());
  30.  
  31. // Causes "error: use of deleted function ‘Foo<T>::Foo(R&&, typename std::enable_if"
  32. //Foo<short> d("failed");
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Initialized from rvalue reference: 0
Initialized from rvalue reference: 1
Initialized from rvalue reference: 1