rvalue

literal and rvalue reference

房东的猫 提交于 2019-11-28 08:00:39
问题 void test(int && val) { val=4; } void main() { test(1); std::cin.ignore(); } Is a int is created when test is called or by default in c++ literals are int type? 回答1: Note that your code would compile only with C++11 compiler. When you pass an integral literal, which is by default of int type, unless you write 1L , a temporary object of type int is created which is bound to the parameter of the function. It's like the first from the following initializations: int && x = 1; //ok. valid in C++11

lvalue binding to rvalue reference

旧街凉风 提交于 2019-11-28 07:54:55
问题 I am trying to understand how lvalues bind to rvalue references. Consider this code: #include <iostream> template<typename T> void f(T&& x) { std::cout << x; } void g(int&& x) { std::cout << x; } int main() { int x = 4; f(x); g(x); return 0; } While the call to f() is fine, the call to g() gives a compile-time error. Does this kind of binding work only for templates? Why? Can we somehow do it without templates? 回答1: Since T is a template argument, T&& becomes a forwarding-reference . Due to

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

牧云@^-^@ 提交于 2019-11-28 07:46:47
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; Alexander Gessler 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 is a l-value - so you can assign to it. int a = 4, b = 5; int& f() {return a;} ... f() = 6;

Function that accepts both lvalue and rvalue arguments

若如初见. 提交于 2019-11-28 07:13:51
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 void print_stream(istream& is); then the second use won't compile because an rvalue will not bind to a

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

拟墨画扇 提交于 2019-11-28 06:51:39
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 talk a little about the things I've been trying so far. I want to be able to do this check at

rvalues and temporary objects in the FCD

北城以北 提交于 2019-11-28 04:09:36
问题 It took me quite some time to understand the difference between an rvalue and a temporary object. But now the final committee draft states on page 75: An rvalue [...] is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object. I can't believe my eyes. This must be an error, right? To clarify, here is how I understand the terms: #include <string> void foo(std::string&& str) { std::cout << str << std::endl; } int main() { foo(std::string("hello")); }

Why don't rvalues have an address?

早过忘川 提交于 2019-11-28 03:55:56
问题 Why don't rvalues have a memory address? Are they not loaded into the RAM when the program executes or does it refer to the values stored in processor registers? 回答1: Your question ("Why don't rvalues have a memory address?") is a bit confused. An rvalue is a kind of expression. Expressions don't have addresses: objects have addresses. It would be more correct to ask "why can one not apply the address-of operator to an rvalue expression?" The answer to that is rather simple: you can only take

error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool>::reference {aka std::_Bit_reference}’

て烟熏妆下的殇ゞ 提交于 2019-11-28 03:43:00
问题 Why do I get the error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector::reference {aka std::_Bit_reference}’? vector<vector<bool>> vis; bool& visited(int x, int y) { return vis[x][y]; //error } As far as I know operator[] in vector returns reference, so it should be an lvalue, but it doesn't work. What should I do to make it work? 回答1: That's because std::vector< bool > is not what it looks like. There's a specialization for std::vector with

C++0x const RValue reference as function parameter

岁酱吖の 提交于 2019-11-28 00:38:47
I am trying to understand why someone would write a function that takes a const rvalue reference . In the code example below what purpose is the const rvalue reference function (returning "3"). And why does overload resolution preference the const Rvalue above the const LValue reference function (returning "2"). #include <string> #include <vector> #include <iostream> std::vector<std::string> createVector() { return std::vector<std::string>(); } //takes movable rvalue void func(std::vector<std::string> &&p) { std::cout << "1"; } //takes const lvalue void func(const std::vector<std::string> &p)

lvalue required as increment operand error

筅森魡賤 提交于 2019-11-28 00:09:19
#include <stdio.h> int main() { int i = 10; printf("%d\n", ++(-i)); // <-- Error Here } What is wrong with ++(-i) ? Please clarify. -i generates a temporary and you can't apply ++ on a temporary(generated as a result of an rvalue expression). Pre increment ++ requires its operand to be an lvalue, -i isn't an lvalue so you get the error. The ++ operator increments a variable. (Or, to be more precise, an lvalue —something that can appear on the l eft side of an assignment expression) (-i) isn't a variable, so it doesn't make sense to increment it. Lee Louviere You can't increment a temporary