Are there any articles about when it is a good practice to use const references as return types?
This is less of a question about a particular issue and more of an educative question.
We are in the process of migrating to modern C++ from a somewhat rudimentary level of C and me and my colleague are tasked with mentoring a group of other people on C++ since we are pretty comfortable with the language.
I am having a slight argument with said colleague about this subject. The example at hand is a simple class:
class Cls
{
private:
vector<int> vec;
public:
Cls() { /* put 3 random values in vec */ }
const vector<int>& getVec() {return vec; }
};
My argument from the fact that:
References should be used as returns since you don't lose time copy-ing stuff. It's straight-up speedup.
References should be const since if you return just a reference to
vec
then anyone can mutate it using just the getter, which is obviously bad. Hence we need to returnconst TYPE&
for getters.
My colleague supports just returning vector<int>
and be done with it. His arguments are:
- It's an advanced concept that should not be taught from the start. We can just return by value from getters for now (and thus produce copies) and make the transition to reference later, when they are capable of understanding what happens behind the scenes. (My opinion being that since I consider using this to be very good practice it should be taught and enforced from the start so people get used to doing it, and then the part where we make sure everybody really understands what const references mean can come later since we might not have time to get the mentees to the level where can juggle references.)
So my questions basically are:
Are there some articles about good practices on this subject and when to use const references instead of values as returns?
While me and my colleague are fairly comfortable with C++ neither of us has worked professionally with it so... is there a standard or convention in the industry for this question?