2

This code looks weird:

const double square(double x) {

    return x*x;

}

Common sense tells me that const in this context means either

  • the function returns a const double

OR

  • it promises not to change the double that was passed in. But it was passed in by value! I don't understand

OR

  • I know that const member functions promise not to change *this, but this is not even a member function.

Edit

What's the point to return a const double if you can save the result in a non-const variable and edit it??

double var = square(4.5); // no compile error

var = 0.3;
Oleksiy
  • 37,477
  • 22
  • 74
  • 122
  • 2
    This function returns a const double, which doesn't make a whole lot of sense since the calling function can always store the result in a non-const double (the value will be copied). I think some compilers raise warnings regarding that kind of return type. – Nbr44 Aug 09 '13 at 08:31
  • 2
    *"Common sense tells me ..."*. Please don't use *only* your common sense, that is not enough when learning a programming language (especially C++). Read a good C++ book as well. – Nawaz Aug 09 '13 at 08:32
  • In OOP this means that an class extending the class this function is contained in, cannot override this function. – LuigiEdlCarno Aug 09 '13 at 08:33
  • 2
    @LuigiEdlCarno: this is not member function, so OOP does not apply – mvp Aug 09 '13 at 08:34
  • 3
    It's NOT a duplicate! Every other question is about **references**, I'm asking about **returning a constant value** – Oleksiy Aug 09 '13 at 08:35
  • @LuigiEdlCarno even if it were a member function, what you say would not be correct. – juanchopanza Aug 09 '13 at 08:36
  • @LuigiEdlCarno, You're thinking of `final`. – chris Aug 09 '13 at 08:37
  • The duplicate answers your original question. Your edit poses a different question. – juanchopanza Aug 09 '13 at 08:39
  • From my answer to that question: "The const at the beginning means whatever is being returned is const.". It doesn't matter if it is a reference. – juanchopanza Aug 09 '13 at 08:44
  • I'd answer this, but I can't, because it is marked as a duplicate. (It's not, and none of the answers to the questions actually answer this; for the most part, they're not even correct.) – James Kanze Aug 09 '13 at 09:00

5 Answers5

4

It is weird. As you say:

the function returns a const double

For a user-defined type, this would prevent you from modifying the return value:

square(2) += 5; // Error

although, as noted in the comments, this isn't allowed for a built-in type like double anyway. Even for user-defined types, while you probably don't want to write code like that, there's no point preventing it either.

For more complicated types, returning a const value can actually be harmful: it inhibits move semantics, since a const object can't be moved from. This could hurt performance.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    The answer is misleading : `square(2) += 5;` will be an error even if the return type is non-const double. The error has nothing to do with `const` in the return type. – Nawaz Aug 09 '13 at 08:43
  • square(2) += 5; results in an error anyway, regardless of const being there or not – Oleksiy Aug 09 '13 at 08:44
  • @Nawaz: You're right (for built-in types); perhaps I shouldn't have tried to demonstrate what this `const` might actually achieve, and just focussed on why you don't want it. – Mike Seymour Aug 09 '13 at 08:46
  • @Nawaz: I hope the wording is less misleading now. – Mike Seymour Aug 09 '13 at 08:54
0

That is not a "const function". It is a function which returns a value that is const. A "const function" only applies to member functions, and then the const goes on the right, after the parameter list.

The utility of this for a simple value type is dubious. However, if you were returning a reference to const or a const pointer, or something else, then it could have merit.

The only thing that returning a const of a simple type by value does is make sure that it can't be captured by non-const r-value reference in C++11. Otherwise, they'll just copy the value and be on their way.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0

In the example you give, and for any POD type, it makes little sense, but for more complex types it can make sense:-

const SomeClass &SomeFunction ();

which returns a reference to a const object.

Skizz
  • 69,698
  • 10
  • 71
  • 108
  • Not the best example- this return type looks like `T& SomeFunction()` with `T` being `const SomeClass`. If we were talking pointers, it would look like `const SomeClass* SomeFunction()` while the OP is asking about cases like `SomeClass* const SomeFunction()`. – Kos Aug 09 '13 at 08:37
  • What if it's a POD type and you return by reference? – Luchian Grigore Aug 09 '13 at 08:46
  • @LuchianGrigore: references to PODs would behave the same as a reference to a class, so a const reference would be (usually) unchangeable by the caller. The returned type in these cases is a reference to something. – Skizz Aug 09 '13 at 08:56
  • Which means the statement "In the example you give, and for any POD type, it makes little sense", is wrong. It doesn't matter if it is or isn't a POD, what matters is whether you return by value or not. – Luchian Grigore Aug 09 '13 at 09:18
  • @LuchianGrigore: Hmmm....just trying out some stuff and you're right, returning a class by value can also lose the const. I can't find any way to prevent copying to non-const objects. – Skizz Aug 09 '13 at 09:44
0

A const function with const before the return value is usually used where the function returns a pointer (or perhaps a reference) to say that it will only return const pointers. This one returns a double and that means it only returns doubles that are const.

David Elliman
  • 1,379
  • 8
  • 15
-1

It returns a const double type variable.

If you do it like this, most compilers give a warning if you try to assign the returned value to a double type.

Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76