fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. enum class Mutability {Const, Non_Const};
  5.  
  6. template<Mutability T>
  7. struct ConstMutability: std::true_type{};
  8.  
  9. template<>
  10. struct ConstMutability<Mutability::Non_Const>: std::false_type{};
  11.  
  12. template <typename T, Mutability U>
  13. class Obj
  14. {
  15. public:
  16. template <Mutability V, typename std::enable_if<
  17. std::is_same<ConstMutability<U>, ConstMutability<V>>::value ||
  18. (ConstMutability<V>::value && !ConstMutability<U>::value)
  19. >::type* = nullptr>
  20. class Ref
  21. {
  22. public:
  23. Ref() {std::cout << "Successfully created a Ref object" << std::endl;}
  24.  
  25. friend class Obj;
  26. };
  27.  
  28. Obj() {}
  29. };
  30.  
  31.  
  32. int main()
  33. {
  34. Obj<int, Mutability::Const>::Ref<Mutability::Const> test1; //pass
  35. //Obj<int, Mutability::Const>::Ref<Mutability::Non_Const> test2; // fail
  36. Obj<int, Mutability::Non_Const>::Ref<Mutability::Const> test3; // pass
  37. Obj<int, Mutability::Non_Const>::Ref<Mutability::Non_Const> test4; // pass
  38. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Successfully created a Ref object
Successfully created a Ref object
Successfully created a Ref object