0

My question is simple. I was programing a getter when I noticed that I forgot the last const in my expression. But while I was writing it, I doubted: I know that in const function you can't change the returned element, so in the second one the is const std::vector implicit?

const std::vector<int> foo() const{}
std::vector<int> foo() const{}
renton01
  • 49
  • 1
  • 7
  • 2
    you can return whatever you like from a const function, you just cant change the object its invoked on – pm100 Mar 25 '22 at 22:45
  • That's true, I'm going to edit the question @pm100 – renton01 Mar 25 '22 at 22:46
  • I seem to remember returning a `const` value prevents Return Value Optimization, but I'm not sure if that's still true. – François Andrieux Mar 25 '22 at 22:52
  • no the const is not implicit, you are returning a writable vector – pm100 Mar 25 '22 at 22:52
  • the compiler will tell you if you try to do something invalid when you declare a function const – pm100 Mar 25 '22 at 22:54
  • so if I'm returning a writable vector, what guarentee the const at the finish of the function? @pm100 – renton01 Mar 25 '22 at 22:55
  • 1
    Since the returned `vector` is a value, a copy, you can fold it, spindle it, and mutilate it without affecting the instance you got the copy from. The trailing `const` is satisfied. – user4581301 Mar 25 '22 at 22:56
  • 1
    the fact that you did not change this inside foo. https://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-function-declaration-of-a-class – pm100 Mar 25 '22 at 23:00
  • 1
    It will be returning a copy by the way and that's missing from the body also there shouldn't be semicolons at the ends there. – QuentinUK Mar 26 '22 at 00:01

2 Answers2

1

You can return non-const values. You cannot return a non-const reference. Both of these functions return a copy, thus the const for the return type is not required.

If you were to return

std::vector<int> &

Then you would need the const.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
1

Your concern is that if you are rturning a reference to data from within the object does that have to be const. Yes. This

class Test {
    int m_num;
public:
    int& getNum()   const { return m_num; }
};

does not compile. You need

class Test {
    int m_num;
public:
    const int& getNum()   const { return m_num; }
};

your case with a vector

class Test {
    int m_num;
    std::vector<int> m_vec;
public:
    const int& getNum()   const { return m_num; }
    std::vector<int> getVec()   const { return m_vec; }
};

works fine because you are returning a copy

but this is not ok

class Test {
    int m_num;
    std::vector<int> m_vec;
public:
    const int& getNum()   const { return m_num; }
    std::vector<int> & getVec()   const { return m_vec; }
};

has to be

class Test {
    int m_num;
    std::vector<int> m_vec;
public:
    const int& getNum()   const { return m_num; }
    const std::vector<int> & getVec()   const { return m_vec; }
};
user4581301
  • 33,082
  • 7
  • 33
  • 54
pm100
  • 48,078
  • 23
  • 82
  • 145