apb_developer
apb_developer

Reputation: 321

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++?

Upvotes: 1

Views: 1011

Answers (2)

Krapow
Krapow

Reputation: 641

the const keyword is left associative, but in "const string &str", since there's no left keyword to qualify, it apply to the word at his right (so string).

It is the same but it's not parsed the same.

Upvotes: 4

Shoe
Shoe

Reputation: 76240

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.

Upvotes: 7

Related Questions