rvalue

Why is pass by value and pass by rvalue overload c++ function call ambiguous?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 02:25:52
问题 If I have, void foo(Bar c); void foo(Bar&& c); foo(Bar()); why is the call to 'foo' is ambiguous? Isn't Bar() in the foo argument clearly an rValue? 回答1: Binding to a reference is an "exact match", as is binding to a non-reference, so both overloads are equally good. In Standardese, this is 13.3.3.1.4 ("Reference binding", [over.ics.ref]): When a parameter of reference type binds directly (8.5.3) to an argument expression, the implicit conversion sequence is the identity conversion [...] 来源:

Is it necessary to have a temporary or a literal to have an rvalue?

自古美人都是妖i 提交于 2019-12-11 08:13:30
问题 This question asks if all temporaries are rvalue. The answer is no, because if we consider this expression: const int &ri = 2 + 3; then, the very same temporary (2 + 3) , which is an rvalue here, can be used as an lvalue in a subsequent expression: const int *pi = &ri; so this temporary is not (only) an rvalue. The logic statement temporary ==> rvalue is then false. However, we cannot write const int &ri = &(2 + 3); // illegal, 2 + 3 -> temporary -> rvalue or int *i = &4; // illegal, 4 is an

Take a reference if lvalue and make a copy if rvalue i.e. make rvalue persistent

落爺英雄遲暮 提交于 2019-12-11 07:59:31
问题 I'm pretty new in move and lvalue semantics. And I have the impression I'm doing it wrong. Here the code I want to be able to write once FunctContainer is implemented: std::function<double(double)> f = [](double x){return (x * x - 1); }; FunctContainer fc1 = FunctContainer(f); FunctContainer fc2 = FunctContainer([](double x){return (x * x - 1); }); I want to write FunctContainer 's ctors so that the lifetime of the function stored in fc1 is the one of f and the lifetime in fc2 of the

Is *p an lvalue or rvalue

回眸只為那壹抹淺笑 提交于 2019-12-11 05:42:34
问题 In the following code, is *a an rvalue or an lvalue? #include <stdio.h> void main() { int b=2; int *a=NULL; a=&b; *a=3; printf("%d",*a); } 回答1: As exposed in http://en.wikipedia.org/wiki/Value_%28computer_science%29 : Lvalues have memory addresses that are programmatically accessible to the running program (e.g., via some address-of–operator like "&" in C/C++), meaning that they are variables or dereferenced references to a certain memory location. Rvalues can be lvalues (see below) or non

std::thread constructor pass by reference when using a class member function

旧时模样 提交于 2019-12-11 04:37:24
问题 I've done quite a bit of research on the new c++11 rvalue and carried over lvalue. Here is a sample of what I have found and read: what-does-t-double-ampersand-mean-in-c11 how-stdthread-constructor-detects-rvalue-reference stdthread-and-rvalue-reference I have also briefed myself on rvalue references Move_Semantics rvalue_references A Proposal to Add an Rvalue Reference to the C++ Language Specifically, regarding the std::thread constructor, I found how-to-create-a-thread-inside-a-class

C++ function with reference argument that works for lvalues and rvalues

孤街醉人 提交于 2019-12-11 03:43:08
问题 I would like to have a C++ function which takes an argument, that's a reference, and works for both lvalues and rvalues with the same syntax . Take this example: #include <iostream> using namespace std; void triple_lvalue(int &n) { n *= 3; cout << "Inside function: " << n << endl; } void triple_rvalue(int &&n) { n *= 3; cout << "Inside function: " << n << endl; } int main() { int n = 3; triple_lvalue(n); cout << "Outside function: " << n << endl; triple_rvalue(5); } Output: Inside function: 9

Return rvalue reference vs return by value in function return type [duplicate]

南笙酒味 提交于 2019-12-10 21:42:37
问题 This question already has answers here : c++11 Return value optimization or move? [duplicate] (4 answers) Closed 4 years ago . In my code I have a function that constructs a string from a piece of data and then returns it. This string isn't used anywhere else, so it's safe for the receiving side to use move-assignment or move-initialization on it. std::string ReadString(...) { ... return std::string(...) } This is basically what I have. Is there any point in making the function return type

C++11 lvalue, rvalue and std::move()

帅比萌擦擦* 提交于 2019-12-10 17:36:04
问题 I have the following code: #include <iostream> using namespace std; void test(int& a) { cout << "lvalue." << endl; } void test(int&& a) { cout << "rvalue" << endl; } int main(int argc, char *argv[]) { int a = 1; int&& b = 2; test(a); test(1); test(std::move(a)); test(b); } which outputs: lvalue. rvalue lvalue. lvalue. std::move() and int&& are rvalue references, I wonder why test(std::move(a)) and test(b) output lvalue ? Is it related with signature matching and function overloading? 回答1: The

Overload a method in a way that generates a compiler error when called with a temporary

假如想象 提交于 2019-12-10 16:09:56
问题 Perhaps this piece of code will illustrate my intent best: #include <array> template <size_t N> void f(std::array<char, N> arr) { } template <size_t N> void f(std::array<char, N>&& arr) { static_assert(false, "This function may not be called with a temporary."); } f() should compile for lvalues but not for rvalues. This code works with MSVC, but GCC trips on the static_assert even though this overload is never called. So my question is two-fold: how to express my intent properly with modern C

Rvalues in C++03

爷,独闯天下 提交于 2019-12-10 15:35:21
问题 How can you tell whether or not a given parameter is an rvalue in C++03? I'm writing some very generic code and am in need of taking a reference if possible, or constructing a new object otherwise. Can I overload to take by-value as well as by-reference and have the rvalue returns call the by-value function? Or do I have a very sickening feeling that this is why rvalue references are in C++0x? Edit: is_rvalue = !(is_reference || is_pointer) ? 回答1: There apparently is a way to determine