-3

I tried to use constants when returning a value from a function/method, but I don't see any benefits of using it because we can't modify the returned value from inside a function. Or even from outside.

class Foo {
    std::string m_val;
public:
    void set(const std::string v) {
        m_val = v;
    }

    const std::string get() const {
        return m_val;
    }
};

int main() {

    Foo f;
    f.set("Hello world!");
    
    /* (1) Wrong
    f.get() = "Thanks";  
    */
    
    /* (2) You can change the "retVal" value except if you used const with "retVal".
    std::string retVal = f.get();
    retVal = "Thank you";
    */

    return 0;
}

Maybe returning a const value is not useless, as I think, but I still didn't find anything that changes my mind, so I asked my question here to clarify that.

6
  • 1
    Note that the returned string is copied, which is why you can do: std::string s = f.get(); s = "a";. Commented Jul 30 at 13:22
  • stackoverflow.com/questions/8716330/… Commented Jul 30 at 13:22
  • 1
    There's no point in making it const. What you should be is to return a const& instead. Commented Jul 30 at 13:24
  • I remember that once I read a recommendation to make return values const to prevent silly mistakes of the caller but that was long before there was move semantics Commented Jul 30 at 13:29
  • 1
    Nitpick, but if you want to get and nakedly modify m_val, it might as well be public. Your attempt to modify the member through get() should always be called out during code review as well becuase either the function is poorly named or you're doing it wrong. Commented Jul 30 at 13:51

2 Answers 2

2

clang-tidy (one of static code analysers) agrees that it is useless and has specific check to remove that: https://clang.llvm.org/extra/clang-tidy/checks/readability/const-return-type.html. Quoting their explanation:

Such use of const is usually superfluous, and can prevent valuable compiler optimizations.


Now, your get() example is actually the only potential upside of returning by const value - it prevents writing to a temporary. However I'd argue that it's only useful if there's a valid reason user would believe get() returns a reference - e.g., your entire project uses get() that returns a reference and suddenly one class has to return a copy. Then returning const value would prevent them from using this specific get() in a wrong way.

Sign up to request clarification or add additional context in comments.

Comments

1

Returning const object is not completely useless. It forbids to call non-const method on returned object

so with

std::string get() const { return m_val; }
const std::string get_const() const { return m_val; }

We have

f.get().push_back('*'); // ok, but useless, temporary is destroyed just after
f.get_const().push_back('*'); // KO

It is generally simpler/better to enforce the restriction by ref qualify the method (of the returned object (so not possible for (std) library)).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.