rvalue-reference Questions

2

In Effective Modern C++ item 12, there is a sample code about the C++11 functions' reference qualifiers: class Widget { public: using DataType = std::vector<double>; … DataType& ...
Edging asked 26/1, 2019 at 13:48

1

Solved

I have a really simple function definition here: void testRvalue(int&& r) { printf("rvalue ref is called\n"); testRvalue(r); // this line gives "no known conversion f...
Pennsylvanian asked 10/2, 2021 at 5:30

4

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 t...

1

Reading Why does as_const forbid rvalue arguments? I understand that we can't convert a rvalue-ref into an lvalue-ref, of course. But why not move the rvalue-ref into a value and return that, i.e. ...
Lulita asked 14/1, 2021 at 9:47

3

Solved

This is an rvalue reference: void foo(int&& a); It does not bind to lvalues: int i = 42; foo(i); // error This is a universal reference: template<typename T> void bar(T&a...

2

Solved

I have a struct F with a function foo that has different implementation whether F is a temporary or not struct F{ void foo() & { std::cout << "F::foo() &" << std::end...
Preamble asked 14/10, 2020 at 11:28

2

Solved

I'm familiar, that the append in std::string returns std::string& and therefore it do not qualify to be moved from, so the result will not be moved. #include <string> int main() { std:...
Pythagoras asked 21/7, 2020 at 9:48

1

Solved

I have recently learned about r-value references. In order to more thoroughly experiment I decided to write a simple DenseMatrix class. My question is is it possible to write any function ( Transpo...
Gethsemane asked 17/7, 2020 at 16:50

1

Solved

According to en.cppreference.com (from what I can gather): std::is_convertible is a trait class requiring types From & To to be such that a function with return type To that returns a From val...
Unrig asked 29/6, 2020 at 17:58

1

So basically my question is: When should I use an rvalue reference? In this example, I'm working on a logger class (it just logs things to the console...). I have different functions to log message...
Sync asked 3/6, 2020 at 14:48

2

Solved

#include <utility> template <typename Container> decltype(auto) index(Container &&arr, int n) { return std::forward<Container>(arr)[n]; } Make a function call : #inclu...
Soares asked 3/6, 2020 at 5:38

2

Solved

Consider the following function accept that takes a "universal reference" of type T and forwards that to a parse<T>() function object with an overload for lvalues and one for rvalues: templa...
Indulgence asked 25/11, 2013 at 14:44

2

Solved

I've been switching Template Factory functions to use (and understand) std::forward to support rvalues and move semantics. My usual boilerplate factory functions for template classes have always ma...

2

Solved

A note: this is an API design question, riding on the design of the constructors of unique_ptr and share_ptr for the sake of the question, but not aiming to propose any change to their current spec...

5

Solved

I think there's something I'm not quite understanding about rvalue references. Why does the following fail to compile (VS2012) with the error 'foo' : cannot convert parameter 1 from 'int' to 'int &...
Lifework asked 26/9, 2012 at 16:47

3

Solved

Consider the code below void foo(std::vector<int> v) { //do something here } //calling the function vector<int> v1 = {1,2,3}; foo(std::move(v1)); My question is, isn't the function ...
Respiration asked 20/1, 2020 at 20:52

2

Solved

Example: typedef enum Color { RED, GREEN, BLUE } Color; void func(unsigned int& num) { num++; } int main() { Color clr = RED; func(clr); return 0; } I get the following error when I...
Doone asked 31/12, 2019 at 7:6

3

Solved

I found that lvalue lambda closures can always be passed as rvalue function parameters. See the following simple demonstration. #include <iostream> #include <functional> using namesp...
Scotopia asked 12/11, 2019 at 12:23

2

Solved

I was reading a book about data structure implemented in C++, I dont understand a code snippet, it's part of vector class void push_back(object &&x) { //do something objects[size++] = s...

3

Solved

In C++11, value parameters (and other values) enjoy implicit move when returned: A func(A a) { return a; // uses A::A(A&&) if it exists } At least in MSVC 2010, rvalue reference paramet...
Pyromania asked 19/3, 2012 at 22:49

1

Solved

I was always assuming that std::move() on a std::shared_ptr steals the pointer and sets the pointer of the original to nullptr-thus not increasing the reference count. That does not seem to be true...
Arleta asked 9/9, 2019 at 10:29

3

Solved

#include <set> #include <string> #include <cassert> using namespace std::literals; int main() { auto coll = std::set{ "hello"s }; auto s = "hello"s; coll.insert(std::move(s))...

3

Solved

Using the return value of operator* from a "dead" unique_ptr is bad. The following code compiles but results of course in Undefined Behavior: auto& ref = *std::make_unique<int>(...
Scutum asked 24/7, 2019 at 14:36

4

Solved

I read that this is an rvalue and we cannot get its address by applying &this. In my code, I have tried to use a reference binding to this. I'm wondering which way will give the address of thi...
Footstep asked 1/7, 2019 at 9:16

1

Solved

Let's consider the following code: class X { std::vector<int> _v; public: X(std::vector<int>&& v): _v(std::move(v)) {} }; The compiler calls this constructor only for objec...
Popelka asked 30/6, 2019 at 18:24

© 2022 - 2024 — McMap. All rights reserved.