move-semantics Questions
2
Solved
What is the reason for having these traits in a container (https://en.cppreference.com/w/cpp/memory/allocator_traits)
propagate_on_container_copy_assignment Alloc::propagate_on_container_copy_assig...
Sentimental asked 15/2, 2019 at 6:27
1
Solved
So here i got a small test program:
#include <string>
#include <iostream>
#include <memory>
#include <vector>
class Test
{
public:
Test(const std::vector<int>& a...
Insouciance asked 29/1, 2019 at 11:53
7
Solved
Since we have move semantics in C++, nowadays it is usual to do
void set_a(A a) { _a = std::move(a); }
The reasoning is that if a is an rvalue, the copy will be elided and there will be just one...
Mclaurin asked 10/1, 2014 at 2:56
3
Solved
As you can see in the output, the objects of the vector pre not only "moved" to the vector post, but also preserved their original address space in memory. What is really going on behind this move?...
Tyrothricin asked 23/12, 2018 at 15:25
1
Solved
Rust has the concepts of ownership and borrowing. If a function doesn't borrow its parameter as a reference, the arguments to that function are moved and will be deallocated once they go out of sco...
Mal asked 16/12, 2018 at 15:59
2
Solved
I have a set of unique_ptr instances and want to pass all of them as an argument to a function. Example demonstrated by below code.
#include <memory>
#include <set>
#include <vector...
Adjustment asked 13/8, 2013 at 5:35
3
Solved
Consider this snippet:
class X;
void MoveAppend(vector<X>& src, vector<X>& dst) {
dst.reserve(dst.size() + src.size());
for (const X& x : src) dst.push_back(x);
src.cle...
Destrier asked 9/6, 2013 at 13:18
6
Solved
The MSDN article, How to: Write a Move Constuctor, has the following recommendation.
If you provide both a move constructor and a move assignment operator for your class, you can eliminate redun...
Lowering asked 14/6, 2013 at 22:36
1
Solved
I am trying to implement a rule of five for the first time. After reading a lot of recommendation about best practices, I end up with a solution where copy/move assignment operators seem to be in s...
Dree asked 6/12, 2018 at 13:35
3
Solved
I am trying to understand the move semantics are looking in to compiler generated move constructors (copy and assignment).
In Modern Effective C++, Scott Meyers says in Item #17 that if no explicit...
Peadar asked 14/11, 2018 at 2:35
1
Solved
I'm trying to pass in a closure to a function that will then mutate something passed into it within the scope of the function. Based on my current understanding of Rust, that should look something ...
Workhorse asked 9/11, 2018 at 22:1
1
Solved
I'm passing a lambda to a function which accepts it as a r-value reference.
If my lambda is defined in the function-call itself, I don't really care what happens to it later.
But if my lambda is a...
Barnsley asked 4/11, 2018 at 21:47
2
Solved
struct big_struct{
vector<int> a_vector;
map<string, int> a_map;
};
big_struct make_data(){
big_struct return_this;
// do stuff, build that data, etc
return return_this;
}
int mai...
Calgary asked 29/10, 2018 at 14:59
3
Solved
I've searched but cannot find the answer to "When" to use them. I just keep hearing that it's good because it saves me that extra copy. I went around putting it in every class I had but some how th...
Jurel asked 18/6, 2012 at 4:56
2
Solved
Both libstdc++ and libc++ makes moved-from std::string object empty, even if the original stored string is short and short string optimization is applied. It seems to me that this emptying makes an...
Lazurite asked 8/10, 2018 at 6:18
2
Solved
I want to convert a temporary container to a std::map<S, T>.
Let's say the temporary container is a std::unordered_map<S, T>, with T move-constructible.
My question is: (how) can I us...
Foilsman asked 15/10, 2018 at 5:25
2
Solved
Motivation:
I'm trying to transfer a std::vector<std::unique_ptr<some_type>> to a different thread, via a lambda capture.
Since I need the vector to not be cleaned up when the functio...
Havens asked 30/9, 2018 at 15:51
1
Solved
I am using Howard Hinnant's nice little arena-based allocator, short_alloc.
It struck me that move-assigning from a vector, which has outgrown its arena and is thus allocated on heap, could be don...
Counselor asked 7/10, 2013 at 16:46
3
Solved
To my understanding, move-constructors and move-assign must be marked noexcept in order for the compiler to utilize them when, for example, reallocating inside a vector.
However, is there any real...
Kaiserslautern asked 7/11, 2013 at 10:16
2
Solved
Take for example this code:
#include <type_traits>
#include <iostream>
struct Foo
{
Foo() = default;
Foo(Foo&&) = delete;
Foo(const Foo&) noexcept
{
std::cout <<...
Jolly asked 17/8, 2018 at 19:33
1
Solved
Trying to provide a solution to std::string_view and std::string in std::unordered_set, I'm playing around with replacing std::unordered_set<std::string> with std::unordered_map<std::strin...
Smile asked 8/8, 2018 at 17:9
1
Solved
I have the following factory function:
auto factory() -> std::tuple<bool, std::vector<int>>
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
return { true, vec...
Kinnon asked 25/7, 2018 at 14:9
6
Solved
Minimal working example.
#include <cassert>
#include <list>
#include <queue>
//#define USE_PQ
struct MyClass
{
const char* str;
MyClass(const char* _str) : str(_str) {}
MyCla...
Mckeever asked 22/11, 2013 at 16:14
2
Solved
C++'s STL priority queue have a void pop() method, and a const ref top() method. Thus, if you want to move elements out of the queue, you have to do something like this:
T moved = std::move(const_...
Strongminded asked 26/2, 2014 at 16:52
2
Solved
I recently started using c++ and I chose to learn c++11 features.
But how c++ codes run is sometimes not so tangible.
below is my code.
At the part with decltype(std::move(sample)) sample2 = std...
Alessandraalessandria asked 16/7, 2018 at 15:15
© 2022 - 2024 — McMap. All rights reserved.