move-semantics Questions

2

Solved

Given base class A and derived class B, A has deleted move constructor: class A { public: A() {} A(const A&) = default; A(A&&) = delete; }; class B : public A { }; In such case,...
Ib asked 15/2, 2023 at 4:34

9

Solved

What is it? What does it do? When should it be used? Good links are appreciated.
Apostatize asked 5/8, 2010 at 9:42

2

Solved

Can someone explain the execution order of this code? struct Foo { ~Foo() { std::cout << "1"; } }; int main() { const Foo& bar = Foo(); const Foo& baz = std::move(Foo(...
Tension asked 7/2, 2023 at 10:56

1

Solved

The following function (in my intention) is to take an rvalue and present it as it was an lvalue. auto constexpr RtoL = [](auto&& r) -> decltype(auto) { static_assert(std::is_rvalue_ref...
Haemato asked 24/11, 2021 at 8:25

2

Solved

#include <sstream> #include <string> using namespace std; template<typename T> string ToString(const T& obj) { ostringstream oss; oss << obj; // // oss will never...
Leguminous asked 8/12, 2016 at 8:50

6

Solved

After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable, I starting wondering if there are use cases for a class which can be copied bu...

2

Solved

std::string str1{"This is a test string"}; std::string str2{std::move(str1)}; str1 = "This is a new content of this string"; // Use str1... Am I correct thinking that str1 is i...
Auberbach asked 24/12, 2022 at 6:59

8

Solved

If I pass the following code through my GCC 4.7 snapshot, it tries to copy the unique_ptrs into the vector. #include <vector> #include <memory> int main() { using move_only = std::un...
Humbuggery asked 12/12, 2011 at 0:49

4

Solved

I want to provide zero-copy, move based API. I want to move a string from thread A into thread B. Ideologically it seems that move shall be able to simply pass\move data from instance A into new in...
Privation asked 22/4, 2015 at 19:6

2

I have a fixed-size array [T; SIZE] of values of a type T that is ordered (it implements Ord, but not necessarily Clone or Default). I would like to extract the smallest value of the array and drop...
Rotation asked 30/7, 2020 at 9:27

2

Solved

Consider the following program: #include <vector> #include <iostream> class A { int x; public: A(int n) noexcept : x(n) { std::cout << "ctor with value\n"; } A(const...
Bangweulu asked 10/10, 2022 at 8:32

7

Solved

unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr<T> from a function and assign the returned value to a variable. #incl...
Whirlwind asked 30/11, 2010 at 17:44

1

Solved

I was wondering if it's safe to write a reset method of some class A by using the move assignment operator on this. So instead of A a{}; ... // Do something with a a = A(); if I could write A a{};...
Triumvirate asked 10/6, 2022 at 13:51

2

Solved

In The C++ programming language Edition 4 there is an example of a vector implementation, see relevant code at the end of the message. uninitialized_move() initializes new T objects into the new m...

1

I've run into this weird situation: template <typename pointed_t> class MyPointer {public: MyPointer() : pPointed(nullptr) {} /* PREVENT COMPILER-GENERATED FUNCTIONS */ MyPointer(const MyP...
Congenital asked 6/6, 2022 at 13:55

6

Solved

The Rust language website claims move semantics as one of the features of the language. But I can't see how move semantics are implemented in Rust. Rust boxes are the only place where move semantic...
Cm asked 7/4, 2015 at 11:37

10

Solved

I have a class with a unique_ptr member. class Foo { private: std::unique_ptr<Bar> bar; ... }; The Bar is a third party class that has a create() function and a destroy() function. If I...
Armoury asked 27/9, 2013 at 14:31

3

Solved

Synopsis How can I safely design a move constructor when a class uses multiple inheritance? Details Consider the following scenario: struct T { }; struct U { }; struct X : public T, public U {...

1

Solved

I am testing a custom bi-directional iterator in rust but i ran into error error[E0382]: borrow of moved value: `iter` --> src/main.rs:40:13 | 37 | let mut iter = BiIterator::from(vec![1, 2, 3...
Bettinabettine asked 27/1, 2022 at 7:54

2

Solved

It is known that std::move should not be applied to the function return values because it can prevent RVO (return value optimization). I am interested in the question what should we do if we certai...

4

Solved

In my Fedora 34 environment (g++), std::accumulate is defined as: template<typename ITER, typename T> constexpr inline T accumulate(ITER first, ITER last, T init) { for (; first != last; ++f...
Fouquet asked 1/1, 2022 at 20:35

4

Solved

I was under the impression that mutable references (i.e. &mut T) are always moved. That makes perfect sense, since they allow exclusive mutable access. In the following piece of code I assign a...
Inhuman asked 22/8, 2015 at 9:15

2

Solved

I am trying to get the grasp of rvalue references and move semantics with a simple self-made example but I can't understand a specific part. I have created the following class: class A { public: A...
Flowerer asked 15/12, 2021 at 13:59

2

Why does the following code compile? #include <vector> #include <iostream> struct Foo { std::vector<int> bar = {1, 2, 3}; }; int main() { Foo foo1; const Foo& foo2 = foo1...
Doorsill asked 9/12, 2021 at 16:54

3

I have a Vec<(String, i64)> and need to iterate over the Strings and move them and then iterate over the i64s. However, if I move the Strings I have to store the i64 again into another Vec: l...
Fulvia asked 7/12, 2021 at 13:12

© 2022 - 2024 — McMap. All rights reserved.