rvalue

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[]>

What does static_cast<T> do to a T&?

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:08:13
问题 So I asked this question and I was tinkering around with solving it via static_cast . (Incidentally it does solve the problem, I'm just not sure if I understand why.) In the code: vector<int> foo = {0, 42, 0, 42, 0, 42}; replace(begin(foo), end(foo), static_cast<int>(foo.front()), 13); Is the static_cast simply constructing an R-Value int ? What's the difference between that and just the call: replace(begin(foo), end(foo), int{foo.front()}, 13); EDIT: As inferred by the answers static_cast

C++03. Test for rvalue-vs-lvalue at compile-time, not just at runtime

南楼画角 提交于 2019-11-27 05:42:53
问题 In C++03, Boost's Foreach, using this interesting technique, can detect at run-time whether an expression is an lvalue or an rvalue. (I found that via this StackOverflow question: Rvalues in C++03 ) Here's a demo of this working at run-time (This is a more basic question that arose while I was thinking about this other recent question of mine. An answer to this might help us answer that other question.) Now that I've spelled out the question, testing rvalue-ness in C++03 at compile-time, I'll

On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule

ε祈祈猫儿з 提交于 2019-11-27 04:15:54
I was reading Thomas Becker's article on rvalue reference and their use. In there he defines what he calls if-it-has-a-name rule: Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue. This sounds very reasonable to me. It also clearly identifies the rvalueness of an rvalue reference. My questions are: Do you agree with this rule? If not, can you give an example where this rule can be violated? If there are no violations of this rule. Can we use this rule to define rvalueness

Passing rvalues through std::bind

北城余情 提交于 2019-11-27 04:13:57
I want to pass an rvalue through std::bind to a function that takes an rvalue reference in C++0x. I can't figure out how to do it. For example: #include <utility> #include <functional> template<class Type> void foo(Type &&value) { Type new_object = std::forward<Type>(value); // move-construct if possible } class Movable { public: Movable(Movable &&) = default; Movable &operator=(Movable &&) = default; }; int main() { auto f = std::bind(foo<Movable>, Movable()); f(); // error, but want the same effect as foo(Movable()) } GManNickG The reason this fails is because when you specify foo<Movable> ,

“l-value required” error

早过忘川 提交于 2019-11-27 03:41:49
问题 When do we get "l-value required" error...while compiling C++ program???(i am using VC++ ) 回答1: An "lvalue" is a value that can be the target of an assignment. The "l" stands for "left", as in the left hand side of the equals sign. An rvalue is the right hand value and produces a value, and cannot be assigned to directly. If you are getting "lvalue required" you have an expression that produces an rvalue when an lvalue is required. For example, a constant is an rvalue but not an lvalue. So: 1

Why is taking the address of a temporary illegal?

半城伤御伤魂 提交于 2019-11-27 02:46:06
问题 I know that the code written below is illegal void doSomething(std::string *s){} int main() { doSomething(&std::string("Hello World")); return 0; } The reason is that we are not allowed to take the address of a temporary object. But my question is WHY? Let us consider the following code class empty{}; int main() { empty x = empty(); //most compilers would elide the temporary return 0; } The accepted answer here mentions "usually the compiler consider the temporary and the copy constructed as

What is an example of a difference in allowed usage or behavior between an xvalue and a prvalue FOR NON-POD objects?

爷,独闯天下 提交于 2019-11-27 02:45:12
问题 What are rvalues, lvalues, xvalues, glvalues, and prvalues? gives a good overview of the taxonomy of rvalues/lvalues, and one of the recent answers to that question (https://stackoverflow.com/a/9552880/368896) stresses the point that prvalues are "like" the old-style rvalues, whereas the new xvalues allow for "lvalue-like" behavior. However, consider the following code: class X {}; X foo() { return X(); } int main() { foo() = X(); // foo() is a prvalue that successfully appears on the lhs }

If a functions return an int, can an int be assigned to it?

走远了吗. 提交于 2019-11-27 02:04:15
问题 If a function returns an int, can it be assigned by an int value? I don't see it makes too much sense to assign a value to a function. int f() {} f() = 1; I noticed that, if the function returns a reference to an int, it is ok. Is it restricted only to int? how about other types? or any other rules? int& f() {} f() = 1; 回答1: The first function returns an integer by-value, which is an r-value . You can't assign to an r-value in general. The second f() returns a reference to an integer, which

Function that accepts both lvalue and rvalue arguments

寵の児 提交于 2019-11-27 01:46:21
问题 Is there a way to write a function in C++ that accepts both lvalue and rvalue arguments, without making it a template? For example, suppose I write a function print_stream that reads from an istream and prints the data that was read to the screen, or something. I think it's reasonable to call print_stream like this: fstream file{"filename"}; print_stream(file); as well as like this: print_stream(fstream{"filename"}); But how do I declare print_stream so that both uses work? If I declare it as