fork download
  1. #include <type_traits>
  2.  
  3. template <typename T>
  4. struct Ref
  5. {
  6. T &t;
  7. Ref(T &_t) : t(_t) {}
  8. Ref(const Ref<typename std::remove_cv<T>::type>& other) : t(other.t) {}
  9. };
  10.  
  11. template <typename T>
  12. Ref<T> MakeRef(T& t) { return {t}; }
  13.  
  14. template <typename T>
  15. Ref<const T> MakeConstRef(const T& t) { return {t}; }
  16.  
  17. int main()
  18. {
  19. int foo = 5;
  20. auto r = MakeConstRef(foo);
  21. auto c = r; //This should be allowed.
  22. auto other = MakeRef(foo); //This should also be allowed.
  23. Ref<const int> baz = other; // This should work, too.
  24. Ref<int> bar = c; //This should fail to compile somehow.
  25. }
  26.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:24:20: error: conversion from ‘Ref<const int>’ to non-scalar type ‘Ref<int>’ requested
stdout
Standard output is empty