Object& const vs. const Object& vs. Object const &

Viewed 45

What is the difference between the following reference to a templated object in C++?

template<typename T>
void myFunction(Object<T>& const obj);

template<typename T>
void myFunction(const Object<T>& obj);

template<typename T>
void myFunction(Object<T> const & obj);
1 Answers
  • Object<T>& const obj - is a const reference to non-const Object<T> (which doesn't make sense, because a reference cannot be rebound, it's always "constant");
  • const Object<T>& obj - a reference to constant Object<T>;
  • Object<T> const& obj - a reference to constant Object<T>, i.e. same as above (the const keyword applies to the right only if it cannot find anything to the left);
Related