当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Is int & foo(); an lvalue?

问题描述:

I know there might be other similar questions but they didn't quite answer my question.

I've been browsing through some slides from a lecture about C++ type deduction and on one of them I found the following statement:

int & foo(); // foo() is lvalue;

At first, I thought it was just wrong - foo is a function, it can't be assigned to, it's not an lvalue. But now I'm thinking the author might have had something different in mind. Namely, that a function call might be an lvalue, not the fuction itself.

So in other words:

  1. is foo an lvalue?
  2. is foo() an lvalue?
  3. can a fuction (not a function call) ever be assigned to (i.e. foo = something;)?
  4. "lvalue is every object/thing that can be assigned to" - is this statement always correct and accurate?

Question 4 requires some more explanation. With it, I'm trying to understand what an lvalue really is. Another definition I've seen states: "lvalues have storage addresses that can be obtained". I'm not sure what storage address exactly is but taking our function foo as an example - we can definitely obtain some address of that fuction. In C++ it's just the name of the function

foo; // returns the address of funtion 'foo'

But at the same time I don't think we can ever assign to foo (question 3). So - is it an lvalue or not?

I'd appreciate it if you answered all 4 points. I'm marking the question as C++ and not C++11 as I believe the question applies to all versions of the language. If there are any differences, however, please mention them.

网友答案:

is foo an lvalue?

Yes. A primary expression that designates a function is an lvalue. However, in some contexts, the function is implicitly converted to a pointer; that pointer is a prvalue (or just an rvalue, before C++11).

is foo() an lvalue?

Yes. The value of a function call is an lvalue if the result is an lvalue reference type, as it is here.

can a fuction (not a function call) ever be assigned to

No. Only modifiable objects can be assigned to.

"lvalue is every object/thing that can be assigned to" - is this statement always correct and accurate?

No. An lvalue is an expression that designates a function or object. It can only be assigned to if it designates a modifiable object.

分享给朋友:
您可能感兴趣的文章:
随机阅读: