rvalue

I think I may have come up with an example of rvalue of array type

匆匆过客 提交于 2019-11-29 01:53:31
C++03 §4.2 N°1: An lvalue or rvalue of type “array of N 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. What has been confusing in this statement for a long time for me was that I didn't quite understand what an rvalue of array type would mean. That is, I couldn't come up with an expression whose type were an array and the result were an rvalue. I read this thread, which basically asks the same question and the accepted answer is "no, there is no rvalue of array type". I think I just might have

Why const for implicit conversion?

不问归期 提交于 2019-11-28 23:32:28
After extensive reading of ISO/IEC 14882, Programming language – C++ I'm still unsure why const is needed for implicit conversion to a user-defined type with a single argument constructor like the following #include <iostream> class X { public: X( int value ) { printf("constructor initialized with %i",value); } } void implicit_conversion_func( const X& value ) { //produces "constructor initialized with 99" } int main (int argc, char * const argv[]) { implicit_conversion_func(99); } Starting with section 4 line 3 An expression e can be implicitly converted to a type T if and only if the

Passing rvalue reference to const lvalue reference paremeter

限于喜欢 提交于 2019-11-28 21:42:43
问题 I am trying to understand C++11 rvalue references and how to use them for optimal performance in my code. Let's say we have a class A that has a member pointer to a large amount of dynamically allocated data. Furthermore, a method foo(const A& a) that does something with an object of class A . I want to prevent the copy constructor of A from being called when an object of A is passed to the function foo , since in that case it will perform a deep copy of the underlying heap data. I tested

How can a returned object be assignable?

元气小坏坏 提交于 2019-11-28 21:21:22
In Effective C++, Item 3, Scott Meyers suggests overloading operator* for a class named Rational : class Rational { ... }; const Rational operator*(const Rational& lhs, const Rational& rhs); The reason for the return value being const -qualified is explained in the following line: if it were not const , programmers could write code such as: (a * b) = c; or, more probably: if (a*b = c) Fair enough. Now I’m confused as I thought that the return value of a function, here operator*, was a rvalue, therefore not assignable. I take it not being assignable because if I had: int foo(); foo() += 3; that

Why pre-increment operator gives rvalue in C?

删除回忆录丶 提交于 2019-11-28 21:09:56
In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why? C doesn't have references. In C++ ++i returns a reference to i (lvalue) whereas in C it returns a copy(incremented). C99 6.5.3.1/2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation . The expression ++Eis equivalent to (E+=1). ‘‘value of an expression’’ <=> rvalue However for historical reasons I think "references not being part of C" could be a possible reason. Nawaz C99 says in the

Regarding lvalue-to-rvalue conversion, when is it required?

£可爱£侵袭症+ 提交于 2019-11-28 12:18:58
I've been reading quite many on the Internet and it seems that many people mentioned the following rules (but i couldn't find it in the standard), The addition operator + (and all other binary operators) requires both operands to be rvalue, and the result is rvalue. And so on.. I checked the C++ standard, and it clearly states that (clause 3.10/2), Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue (clause 5/9), Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue

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

旧巷老猫 提交于 2019-11-28 11:22:34
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 does seem to construct an R-Value int : http://ideone.com/dVPIhD But this code does not work on Visual

Why does std::forward have two overloads?

江枫思渺然 提交于 2019-11-28 10:53:55
Given the following reference collapsing rules T& & --> T& T&& & --> T& T& && --> T& T&& && --> T&& The third and fourth rule imply that T(ref qualifer) && is the identity transformation, i.e. T& stays at T& and T&& stays at T&& . Why do we have two overloads for std::forward ? Couldn't the following definition serve all purposes? template <typename T, typename = std::enable_if_t<!std::is_const<T>::value>> T&& forward(const typename std::remove_reference<T>::type& val) { return static_cast<T&&>(const_cast<T&&>(val)); } Here the only purpose the const std::remove_reference<T>& serves is to not

Why is taking the address of a temporary illegal?

你说的曾经没有我的故事 提交于 2019-11-28 09:09:08
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 two objects that are located in the exact same location of memory and avoid the copy." According to the

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

烂漫一生 提交于 2019-11-28 09:07:32
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 } In this example, the expression foo() is a prvalue that appears on the left-hand side, and accepts