Search code examples
c++parameter-passingconstantsstring-constant

What is the difference between const string & str and string const & str when passed as an argument to a function in c++


I am using pass by reference to const string in a function argument like below:

class Super
{
   void alignment(const string & str);
};

void Super::alignment(const string & str)
{
  //do some stuff
}

Now if I use as below it gives the same result:

void Super::alignment(string const & str)
{
  //do some stuff
}

What is the difference in this two types of definition in c++?


Solution

  • const T& and T const& are semantically the same exact thing. Just like const T* and T const* are semantically the same, but T const* and T* const are different.