1

If I have a member function that looks like this:

const T& temp const( const T& t) const{
    //some code
    return t;
}

What do each of the const do, in the order left to right? Is this right or wrong?

  1. return const object
  2. can't change any of the member fields that are not mutable
  3. can only pass in const objects of type T
  4. this is a const member function, can only be called with a const object
conterio
  • 1,087
  • 2
  • 12
  • 25

1 Answers1

8
const T& temp const( const T& t) const{
^^1^^         ^^2^^  ^^3^^       ^^4^^

In order from left to right:

  1. This function returns a reference to const T. So the return will not be modifiable.
  2. Invalid syntax. Remove.
  3. This function takes as its argument a nonmodifable reference to T. It could be a reference to an existing object. It could be called from a temporary that has its lifetime extended to the lifetime of t. You can't say much about what temp was called with - it could have been an lvalue, it could have been an rvalue (if T is either copy- or move-constructible).
  4. If temp is not a member function, syntax error. Otherwise, it is a const-qualification on the member function, indicating that it can be called on a const object and that the function may not modify any non-mutable members of this or call any other non-const-qualified member functions.
Barry
  • 286,269
  • 29
  • 621
  • 977