lvalue

How to determine programmatically if an expression is rvalue or lvalue in C++?

▼魔方 西西 提交于 2019-11-27 20:26:38
问题 What's the best way to determine if an expression is a rvalue or lvalue in C++? Probably, this is not useful in practice but since I am learning rvalues and lvalues I thought it would be nice to have a function is_lvalue which returns true if the expression passed in input is a lvalue and false otherwise. Example: std::string a("Hello"); is_lvalue(std::string()); // false is_lvalue(a); // true 回答1: Most of the work is already done for you by the stdlib, you just need a function wrapper:

pointer increment and dereference (lvalue required error)

社会主义新天地 提交于 2019-11-27 19:34:14
问题 I am trying to understand how pointer incrementing and dereferencing go together, and I did this to try it out: #include <stdio.h> int main(int argc, char *argv[]) { char *words[] = {"word1","word2"}; printf("%p\n",words); printf("%s\n",*words++); printf("%p\n",words); return 0; } I expected this code to do one of these: First dereference then increase the pointer (printing word1) First dereference then increase the value (printing ord1) Dereference pointer + 1 (printing word2) But compiler

Is there a reason why an array name is not an lvalue?

家住魔仙堡 提交于 2019-11-27 16:45:38
问题 For example, int x[10]; int i = 0; x = &i; //error occurs! According to C - A Reference Manual, an array name cannot be an lvalue. Thus, x cannot be an lvalue. But, what is the reason the array name cannot be an lvalue? For example, why does an error occur in the third line? 回答1: Your reference is incorrect. An array can be an lvalue (but not a modifiable lvalue), and an "array name" (identifier) is always an lvalue. Take your example: int x[10]; int i = 0; x = &i; //error occurs! Apply C11 6

Why are compound literals in C modifiable

你说的曾经没有我的故事 提交于 2019-11-27 09:42:24
One does usually associate 'unmodifiable' with the term literal char* str = "Hello World!"; *str = 'B'; // Bus Error! However when using compound literals, I quickly discovered they are completely modifiable (and locking at the generated machine code, you see they are pushed on the stack): char* str = (char[]){"Hello World"}; *str = 'B'; // A-Okay! I'm compiling with clang-703.0.29 . Shouldn't those two examples generate the exact same machine code? Is a compound literal really a literal, if it's modifiable? EDIT: An even shorter example would be: "Hello World"[0] = 'B'; // Bus Error! (char[])

Casting a pointer does not produce an lvalue. Why?

回眸只為那壹抹淺笑 提交于 2019-11-27 09:15:49
After posting one of my most controversial answers here , I dare to ask a few questions and eventually fill some gaps in my knowledge. Why isn't an expression of the kind ((type_t *) x) considered a valid lvalue, assuming that x itself is a pointer and an lvalue, not just some expression? I know many will say "the standard disallows it", but from a logical standpoint it seems reasonable. What is the reason that the standard disallows it? After all, any two pointers are of the same size and the pointer type is just a compile-time abstraction that indicates the appropriate offset that should be

Is the result of a cast an rvalue?

不羁岁月 提交于 2019-11-27 09:04:09
Let int a = 0; Then is (int)a an rvalue in standard C++? Different compilers show different results for this code: #include <iostream> using namespace std; void f(int& x) { cout << "l value" << endl; } void f(int&& x) { cout << "r value" << endl; } int main() { int a = 0; f((int)a); } compilers with different results: 1) http://cpp.sh/2r6 2) http://webcompiler.cloudapp.net/ Shafik Yaghmour The should be an rvalue but webcompiler is running Visual Studio and Visual Studio has an extension which allows temporary objects to be bound to non-const lvalue references . a bug/extension that casues it

should std::common_type use std::decay?

萝らか妹 提交于 2019-11-27 08:54:05
Given types A,B , I am concerned with the exact definition of std::common_type<A,B> , disregarding the variadic case std::common_type<A...> for arbitrary types A... . So let using T = decltype(true ? std::declval<A>() : std::declval<B>()); using C = std::common_type<A,B>; Now, according to a number of sources, I have found the following relations (skipping typename for brevity): cppreference.com : C::type = std::decay<T>::type cplusplus.com : C::type = T GCC 4.8.1 <type_traits> implementation: C::type = std::decay<T>::type if T is valid, otherwise C does not contain a ::type member ("SFINAE

Array and Rvalue

陌路散爱 提交于 2019-11-27 07:41:21
问题 $4.2/1 - "An lvalue or rvalue of type “array ofN T” or “array of unknown bound of T” can be converted to an rvalue of type “pointer to T.” The result is a pointer to the first element of the array." I am not sure how do we get an rvalue of an array type other than during initialization/declaration? 回答1: I'm not sure what you refer to by "initialization/declaration" in this context. In the following, the array is a prvalue template<typename T> using alias = T; int main() { return alias<int[]>

lvalue required as left operand of assignment

断了今生、忘了曾经 提交于 2019-11-27 05:44:31
问题 Why am I getting lvalue required as left operand of assignment with a single string comparison? How can I fix this in C ? if (strcmp("hello", "hello") = 0) Thanks! 回答1: You need to compare, not assign: if (strcmp("hello", "hello") == 0) ^ Because you want to check if the result of strcmp("hello", "hello") equals to 0 . About the error: lvalue required as left operand of assignment lvalue means an assignable value (variable), and in assignment the left value to the = has to be lvalue (pretty

C++: is return value a L-value?

你说的曾经没有我的故事 提交于 2019-11-27 05:13:09
问题 Consider this code: struct foo { int a; }; foo q() { foo f; f.a =4; return f;} int main() { foo i; i.a = 5; q() = i; } No compiler complains about it, even Clang. Why q() = ... line is correct? 回答1: No, the return value of a function is an l-value if and only if it is a reference (C++03). (5.2.2 [expr.call] / 10) If the type returned were a basic type then this would be a compile error. (5.17 [expr.ass] / 1) The reason that this works is that you are allowed to call member functions (even non