lvalue-to-rvalue

Does the standard mandate an lvalue-to-rvalue conversion of the pointer variable when applying indirection?

我的未来我决定 提交于 2019-12-17 09:52:11
问题 TL;DR Given the following code: int* ptr; *ptr = 0; does *ptr require an lvalue-to-rvalue conversion of ptr before applying indirection? The standard covers the topic of lvalue-to-rvalue in many places but does not seem to specify enough information to determine whether the * operator require such a conversion. Details The lvalue-to-rvalue conversion is covered in N3485 in section 4.1 Lvalue-to-rvalue conversion paragraph 1 and says ( emphasis mine going forward ): A glvalue (3.10) of a non

When a function takes an rvalue reference, what is the type of that variable within the function?

烂漫一生 提交于 2019-12-11 06:05:12
问题 This is a question of terminology. If I have this: #include <vector> void g(std::vector<int>&& arg); void f0(std::vector<int>&& v) { static_assert(std::is_same<decltype(v), std::vector<int>&&>::value); // Looks like v is an rvalue reference. static_assert(std::is_same<decltype((v)), std::vector<int>&>::value); static_assert(std::is_same<std::decay<decltype(v)>::type, std::vector<int>>::value); return g(std::move(v)); // Fine. } then what type is v ? If you are talking about calling f0 , you'd

Does a Comparison Between an Lvalue and a Literal Invoke an Lvalue-to-Rvalue Conversion?

匆匆过客 提交于 2019-12-05 19:57:46
I asked this question: static_assert of const Variable And apparently it comes down to the question does a floating point lvalue get converted to an rvalue for the purposes of comparison? So in this code does an lvalue-to-rvalue conversion occur? const float foo = 13.0F; static_assert(foo > 0.0F, "foo must be greater than 0."); Yes, it is performed. Basically, it's all because 3.0 > 1.2 is a well-formed expression, that contains nothing but prvalues for operands. First, [expr]/9 states (emphasis mine) that Whenever a glvalue expression appears as an operand of an operator that expects a

Why an Rvalue Reference is Turned into Lvalue Reference by a Universal Reference

喜夏-厌秋 提交于 2019-12-01 07:36:39
I suppose when a universal reference parameter is matched with an rvalue reference argument, an rvalue reference argument is returned. However, my testing shows that the rvalue reference is turned into a lvalue reference by the universal reference function template. Why is it so? #include <iostream> #include <type_traits> using namespace std; template <typename T> T f1(T&&t) { //<-----this is a universal reference cout << "is_lvalue reference:" << is_lvalue_reference<T>::value << endl; cout << "is_rvalue reference:" << is_rvalue_reference<T>::value << endl; cout << "is_reference:" << is

Invalid initialization of non-const reference of type

耗尽温柔 提交于 2019-11-30 17:22:43
In the following code, I'm not able to pass a temporary object as argument to the printAge function: struct Person { int age; Person(int _age): age(_age) {} }; void printAge(Person &person) { cout << "Age: " << person.age << endl; } int main () { Person p(50); printAge(Person(50)); // fails! printAge(p); return 0; } The error I get is: error: invalid initialization of non-const reference of type ‘Person&’ from an rvalue of type ‘Person’ I realize that this is something to do with passing an lValue to a function expecting a rValue... Is there a way to convert my lValue to rValue by using std:

Assigning Rvalue returned from function to another Rvalue

。_饼干妹妹 提交于 2019-11-29 19:26:35
问题 class Test { public: int n1; }; Test func() { return Test(); } int main() { func() = Test(); } This doesn't make sense to me. How and why is this allowed? Is it undefined behavior? If a function returns an rvalue, then how is it possible to set an rvalue to another rvalue? If I tried this with any primitive types, it would give me an error like I expect. I know that lvalues are a place in memory, so is the function creating a temporary lvalue (rvalue?) and assigning it to another lvalue? Can

rvalue on the left side

a 夏天 提交于 2019-11-29 18:54:32
问题 Why is this code compiling? I thought that rvalues returned by ctor are not located in memory and therefore can't be used as lvalues. #include <iostream> #include <vector> class Y { public : explicit Y(size_t num = 0) : m_resource {std::vector<int>(num)} { } std::vector<int> m_resource; }; int main(int argc, const char * argv[]) { Y(1) = Y(0); // WHAT?!? return 0; } 回答1: The synthesized assignment operator is declared as one of these (if it can be synthesized and isn't declared as deleted)

Why doesn't C++ move construct rvalue references by default? [duplicate]

不羁的心 提交于 2019-11-29 09:29:33
This question already has an answer here: Rvalue Reference is Treated as an Lvalue? 4 answers Lvalue reference constructor is called instead of rvalue reference constructor 1 answer Say I have the following function void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } Why do I need to cast param back to an rvalue with std::move() ? Shouldn't it be obvious that the type of param is rvalue since it was declared in the function signature as an rvalue reference? Shouldn't the move constructor be automatically invoked here on this principle alone? Why

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

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