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