0

I understand for pointers and objects making them const while returning returns a locked copy of that object so like for objects only const functions can be called on it.

I was wondering is there any sense in returning const int from a function. Since int is just a primitive type I don't see any difference between: const int& myFunction(); and int& myFunction();. Is there any small difference between the two that I am missing here or are they EXACTLY the same and therefore writing const before int& return type is just stupid?

Edit:

#include <vector>

const int& getConstInt() { return 1; }
int& getInt() { return 1; }

std::vector<int> getSimpleVector() { return std::vector<int>(); }
const std::vector<int> getConstVector() { return std::vector<int>(); }

int main() {
    int constInt = getConstInt();
    int simpleInt = getInt();


    constInt = 2; // does not matter that the return type was const
    simpleInt = 2;

    std::vector changedAfterReturn = getConstVector().push_back(3); //locked, gives error
    changedAfterReturn = getSimpleVector().push_back(3); //not locked... works fine.

    getInt() = 2; // This never made any sense in the first place since int is a primitive value.
    getConstInt() = 2; // Neither does this. So whats the use in declaring const.

    return 0;
}

Updated the question to ask for difference between return value of int& vs const int& instead of return by values.

  • Returning a `const` *anything* by value makes no sense. The caller could still assign the value to a non-const variable and modify it at will. – Some programmer dude Sep 09 '22 at 12:05
  • @NathanOliver The linked duplicate asks specifically about non-builtin types. For non-class types the behavior is different (it makes even less sense). There is probably a better matching duplicate somewhere though. In short: The only difference is the type of the function itself. There is no difference in call expressions at all. – user17732522 Sep 09 '22 at 12:09
  • ... not to mention it disables move semantics. So it's a pessimization to boot – StoryTeller - Unslander Monica Sep 09 '22 at 12:10
  • @NathanOliver Nevermind, you added one already. But now the question was edited to ask something else (returning references instead of values). – user17732522 Sep 09 '22 at 12:11
  • *"therefore writing const before int return type is just stupid?"* - Your example does not return an int. Which makes the question nonsensical. Moving the goal post in response to valid closure will tend to do that. – StoryTeller - Unslander Monica Sep 09 '22 at 12:15
  • Both the linked questions ask about returning const objects, while I am asking if there is any sense in returning a const primitive datatype by reference – Chandrachur Mukherjee Sep 09 '22 at 12:17
  • You can't (directly) modify an object through a const reference, so that is the obvious difference. And if the referenced object is of `const` type you can't return a non-`const` reference to it (directly). – user17732522 Sep 09 '22 at 12:18
  • @user17732522 I know that returning const objects is different from returning objects. But I am asking about returning a primitive data type like int or double. – Chandrachur Mukherjee Sep 09 '22 at 12:19
  • @ChandrachurMukherjee I am talking about the difference between returning `int&` and `const int&`, not between `const int` and `int`. – user17732522 Sep 09 '22 at 12:20
  • "Primitive" data are objects in C++. The C++ object model doesn't make a distinction like Java's or C#'s object model may. – StoryTeller - Unslander Monica Sep 09 '22 at 12:20
  • @ChandrachurMukherjee The second answer on the second duplicate has the answer to that: *If the function returns by reference, const protects the returned object from being modified.* – NathanOliver Sep 09 '22 at 12:21
  • @ChandrachurMukherjee Please post a [mcve] as returning a const reference usually leads to a dangling reference as lifetime extension is not transitive. There are exceptions to this __but__ we need to see your code to give an answer. – Richard Critten Sep 09 '22 at 12:22
  • @RichardCritten I added some code explaining my thoughts. Its not really a bug, this question is just to strengthen my c++ concepts. – Chandrachur Mukherjee Sep 09 '22 at 12:24
  • @ChandrachurMukherjee Your code is now again asking about the difference between `const int` and `int` instead of `const int&` and `int&`. For this the duplicates have already been linked which answer the question (although I don't think their quality is particularly good). – user17732522 Sep 09 '22 at 12:28
  • You seemed to have left out the references. – Richard Critten Sep 09 '22 at 12:28
  • @RichardCritten, sorry I had made this code before posting the question so at that time I was not thinking about references. I added them now. – Chandrachur Mukherjee Sep 09 '22 at 12:30
  • 3
    Now with the references `getInt` doesn't compile at all and `getConstInt` should give you a compiler warning that you are returning a dangling reference which causes undefined behavior when accessed. Neither of the functions is correct. – user17732522 Sep 09 '22 at 12:30
  • https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f43-never-directly-or-indirectly-return-a-pointer-or-a-reference-to-a-local-object. – 463035818_is_not_an_ai Sep 09 '22 at 12:43

0 Answers0