lvalue

Why can an rvalue not bind to a non-const lvalue reference, other than the fact that writing to a temporary has no effect?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:32:20
I have read the SO question here and understood this part of the answer: "But if you bind a temporary to a non-const reference, you can keep passing it around "forever" just to have your manipulation of the object disappear, because somewhere along the way you completely forgot this was a temporary." That is, in the following: #include <iostream> void modifyValue(int& rValue) { rValue++; } int main() { modifyValue(9899); return 0; } If an rvalue could bind to a non-const lvalue reference, then potentially many modifications could be done that would eventually be discarded (since an rvalue is

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

error: 'Int' is not convertible to '@lvalue Float'

白昼怎懂夜的黑 提交于 2019-12-05 12:19:17
Given the following function: func greatestCommonDenominator(first: Int, second: Int) -> Int { return second == 0 ? first : greatestCommonDenominator(second, first % second) } And a struct with the following stuff in it: struct Fraction { var numerator: Int var denominator: Int func reduce() { let gcd = greatestCommonDenominator(numerator,denominator) self.numerator /= gcd self.denominator /= gcd } // stuff } I'm getting the following error: error: 'Int' is not convertible to '@lvalue Float' self.numerator /= gcd ^ error: 'Int' is not convertible to '@lvalue Float' self.denominator /= gcd ^ '

A legal array assignment. Is it possible?

[亡魂溺海] 提交于 2019-12-05 05:42:59
After reading the chapter about structures in the K&R book I decided to make some tests to understand them better, so I wrote this piece of code: #include <stdio.h> #include <string.h> struct test func(char *c); struct test { int i ; int j ; char x[20]; }; main(void) { char c[20]; struct {int i ; int j ; char x[20];} a = {5 , 7 , "someString"} , b; c = func("Another string").x; printf("%s\n" , c); } struct test func(char *c) { struct test temp; strcpy(temp.x , c); return temp; } My question is: why is c = func("Another string").x; working (I know that it's illegal, but why is it working)? At

Why myClassObj++++ doesn't incur a compile error : '++' needs l-value just as buildin type do?

╄→гoц情女王★ 提交于 2019-12-05 03:26:19
Why myint++++ compiles fine with VS2008 compiler and gcc 3.42 compiler ?? I was expecting compiler say need lvalue, example see below. struct MyInt { MyInt(int i):m_i(i){} MyInt& operator++() //return reference, return a lvalue { m_i += 1; return *this; } //operator++ need it's operand to be a modifiable lvalue MyInt operator++(int)//return a copy, return a rvalue { MyInt tem(*this); ++(*this); return tem; } int m_i; }; int main() { //control: the buildin type int int i(0); ++++i; //compile ok //i++++; //compile error :'++' needs l-value, this is expected //compare MyInt myint(1); ++++myint;/

C++ Operator Overloading [ ] for lvalue and rvalue

怎甘沉沦 提交于 2019-12-04 16:34:34
I made a class Array which holds an integer array. From the main function, I'm trying to get an element of the array in Array using [ ] as we do for arrays declared in main. I overloaded the operator [ ] as in the following code; the first function returns an lvalue and the second an rvalue (Constructors and other member functions are not shown.) #include <iostream> using namespace std; class Array { public: int& operator[] (const int index) { return a[index]; } int operator[] (const int index) const { return a[index]; } private: int* a; } However, when I try to call those two functions from

c++: function lvalue or rvalue

 ̄綄美尐妖づ 提交于 2019-12-04 08:39:29
I just started learning about rvalue references in c++11 by reading this page , but I got stuck into the very first page. Here is the code I took from that page. int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo() is an lvalue int foobar(); j = foobar(); // ok, foobar() is an rvalue int* p2 = &foobar(); // error, cannot take the address of an rvalue why is foo() an lvalue? is it because foo() returns int& which is basically an lvalue? why is foobar() an rvalue? is it because foobar() returns int ? In general, why would you care if a function is an rvalue or not? I

Does giving data an effective type count as a side-effect?

十年热恋 提交于 2019-12-03 14:08:58
问题 Suppose I have a chunk of dynamically allocated data: void* allocate (size_t n) { void* foo = malloc(n); ... return foo; } I wish to use the data pointed at by foo as a special type, type_t . But I want to do this later, and not during allocation. In order to give the allocated data an effective type , I can therefore do something like: void* allocate (size_t n) { void* foo = malloc(n); (void) *(type_t*)foo; ... return foo } As per C11 6.5/6, this lvalue access should make the effective type

Does giving data an effective type count as a side-effect?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 04:11:23
Suppose I have a chunk of dynamically allocated data: void* allocate (size_t n) { void* foo = malloc(n); ... return foo; } I wish to use the data pointed at by foo as a special type, type_t . But I want to do this later, and not during allocation. In order to give the allocated data an effective type , I can therefore do something like: void* allocate (size_t n) { void* foo = malloc(n); (void) *(type_t*)foo; ... return foo } As per C11 6.5/6, this lvalue access should make the effective type type_t : For all other accesses to an object having no declared type, the effective type of the object

Is std::move(*this) a good pattern?

只愿长相守 提交于 2019-12-03 01:05:56
In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include<iostream> struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun() &&{std::cout << "gun&&" << std::endl;} void fun() const&{gun();} void fun() &&{std::move(*this).gun();} // <-- is this correct? or is there a better option }; int main(){ A a; a.fun(); // prints gun const& A().fun(); // prints gun&& } Something doesn't sound right about it. Is the std::move necessary? Is this a recommended use for it? For the moment if I don