3

This question arouse from the question "why is const in function return type, which is a const pointer, ignored?" (const pointer, not pointer to const)

int* const Foo();

is identical to

int* Foo();

I know that const in function return type, which is a fundamental type, is ignored because const applies to lvalue expressions only.

"const semantics apply to lvalue expressions only"

Function return type, which is a fundamental type, is non-reference function return type, and non-reference function return type is prvalue.

The following expressions are prvalue expressions:

...

However, I cannot find where a function call or an overloaded operator expression, whose return type is pointer belongs to.

Any help will be appreciated. Please correct me if I am understanding wrong.

5
  • 9
    I'm not sure where the confusion lies. That clause applies whenever the return type is not a reference. A pointer is not a reference, so that clause applies to it. It's that simple. Commented Aug 19 at 7:20
  • 1
    Just a note: Your first link points to C language documentation, not C++ Commented Aug 19 at 7:41
  • correct page for C++ would be en.cppreference.com/w/cpp/language/cv.html Commented Aug 19 at 8:06
  • 1
    "const applies to lvalue expressions only" Wrong. Xvalues can be const freely. Prvalues can be const if they are of class or array type: eel.is/c++draft/expr#type-2 Commented Aug 19 at 8:44
  • int* p = Foo(); is allowed for int* const Foo();, because it doesn't matter that Foo is returning a int* const. The const is irrelevant. Commented Aug 19 at 18:54

3 Answers 3

4

These are two different function declarations:

int* foo();
int* const foo();    // error: redeclares function with different return type

Nevertheless, if you have just this declaration

int* const foo();

the const is pointless, because we have this clause in expr.type:

If a prvalue initially has the type “cv T”, where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.

That is, the const-ness of the returned pointer value never plays a role.

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

Comments

1

However, I cannot find where a function call or an overloaded operator expression, whose return type is pointer belongs to.

In order to conclude what value category such a function call belongs to, you simply exclude all other possibilities. I.e. by expr.call/14:

A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise.

int* in int* Foo() is neither lvalue reference, nor rvalue reference type, so calling the function falls into category of prvalues.

Comments

0

int* const Foo(); is the same as int* Foo();.

The const here would apply to the pointer itself, but since the function returns it by value (a prvalue), there’s no object for that const to bind to. The temporary pointer can’t actually be made const, so the qualifier is ignored.

If you want the pointed-to value to be const, you’d write:

const int* Foo();

but int* const Foo(); doesn’t add anything over just int* Foo();.

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.