Skip to main content
Filter by
Sorted by
Tagged with
0 votes
2 answers
234 views

What is the copy-and-swap idiom? lists copy-and-swap as a better alternative to self-assignment check, being simpler and providing stronger exception guarantees. Performance aside, are there any ...
Dominik Kaszewski's user avatar
3 votes
2 answers
219 views

I came across this table describing how the C++ compiler implicitly declares special member functions depending on which ones the user has explicitly declared: Source: Howard Hinnant - How I Declare ...
toliveira's user avatar
  • 1,857
2 votes
1 answer
188 views

I know this may be obvious to many, but please explain it to me: struct MyFoo { MyFoo(int& arg) : ref(arg) {} int& ref; MyFoo& operator= (MyFoo&& other) = default; ...
Zebrafish's user avatar
  • 16.3k
0 votes
1 answer
113 views

A relatively same question is asked before, but the code snippet is different, which I believe makes this question unique. Does the compiler generate the move operations in the following scenario? ...
Ali Sedighi's user avatar
0 votes
0 answers
86 views

I was trying to implement an emplace_back method for a statically-sized array (for fun). I came across the problem of whether I should use placement-new or move assignment for this emplace back method....
timmy george's user avatar
1 vote
0 answers
17 views

Somehow in one of or more of my loops are overrunning my buffer but I can't catch it. It's either that or somehow my capacity or size is at 0 when it shouldn't be but either way I can't find the issue ...
Shahab's user avatar
  • 11
0 votes
0 answers
114 views

In an answer to this question, under the label: "Why does that work?" , it was noted that: Now, if other is being initialized with an rvalue, it will be move-constructed. Perfect. In the ...
CS Student's user avatar
0 votes
1 answer
142 views

If there is function with the following signature: std::vector<std::vector<std::string>> some_func(); And its assigned to a variable of the same type: std::vector<std::vector<std::...
Tanvir's user avatar
  • 885
0 votes
0 answers
166 views

What is the automatically generated move assignment operator for a class Number that contains only one attribute of type std::shared_ptr<Vertex>? #include <iostream> #include <memory>...
GRamon's user avatar
  • 87
1 vote
1 answer
89 views

A simple question but I can not find the set of rules that proves that the behavior of the following code example is correct. It seems here that only strDerived is moved from b, but strBase is copied? ...
SoulfreezerXP's user avatar
1 vote
3 answers
245 views

I made some tests in GCC, Clang and MSVC and found out that emplace_back never calls an assignment operator on the contained class. It only calls the copy or move constructor when reallocation takes ...
really's user avatar
  • 123
4 votes
1 answer
1k views

I recently realized (pretty late in fact) that it's important to have move constructors marked as noexcept, so that std containers are allowed to avoid copying. What puzzles me is why if I do an erase(...
Costantino Grana's user avatar
1 vote
1 answer
2k views

I'm new to C++ and dont know why this is happening and how to fix it. Here's some snippets of the code: header file: class Dictionary{ private: string filename; const ...
Coder-Me's user avatar
0 votes
0 answers
201 views

The following code violates AUTOSAR C++ rule 6-2-1: Move and copy assignment operators shall either move or respectively copy base classes and data members of a class, without any side effects. Move ...
KplnMoon's user avatar
  • 159
6 votes
1 answer
672 views

For move construction: After the move, other is guaranteed to be empty(). 1 For move assignment, the oft-quoted: other is in a valid but unspecified state afterwards. 2 Why is the state of other ...
sleep's user avatar
  • 4,974
1 vote
1 answer
173 views

I want to build my own full Vector class in C++. I started like this: #include <iostream> #include <initializer_list> #define Print(x)(std::cout<< x << std::endl) // Vector ...
DvB's user avatar
  • 41
1 vote
1 answer
163 views

I am trying to clarify-understand move semantics and, for that, I wrote the following code. I used a raw pointer as a data member only to practice in finding all the dangerous spots and also apply ...
AngelosFr's user avatar
  • 137
0 votes
0 answers
236 views

I have a dynamic array in the class. I have two questions: I am wondering how to write move constructor and move assignment operator for dynamic array Tile* mytiles? How can the array be written as a ...
wangmyde's user avatar
5 votes
2 answers
180 views

I have stumbled over code today, that I don't understand. Please consider the following example: #include <iostream> #include <string> class A { public: template <class Type> ...
Benjamin Bihler's user avatar
2 votes
0 answers
1k views

I'm in a situation where I need to implement Move Constructor and Move-Assignment Operator for a class which holds a reference to an object with a deleted Copy Ctor and Copy-Assignment Operator, ...
yano's user avatar
  • 4,523
2 votes
1 answer
294 views

Context: I've a class DLXMatrix with some attribute which are vector of some local class called Header. Each Header holds some pointer on some other Header which refer to elements of the same vector (...
hivert's user avatar
  • 10.7k
7 votes
1 answer
481 views

I wanted to create a list data structure with an iterator class in it. Everything works well but when I declare a move assignment operator the program doesn't compile if it's using the C++14 or C++11 ...
Kanony's user avatar
  • 539
1 vote
1 answer
132 views

I understand how an rvalue would invoke the move constructor and move assignment operator, however I'm confused why the same code in Stroustrup's example below for move assignment isn't used for the ...
notaorb's user avatar
  • 2,210
0 votes
1 answer
70 views

Output is 5500, but why not 5555? class product { public: int b; }; class item { public: int a; item(product& obj) { cout << a; } item() {} void display(...
Karmesh Duggar's user avatar
0 votes
1 answer
206 views

I have a template base class that looks roughly like this Vector.cc: template<typename T, unsigned int D> class Vector { private: T _data[D]; ... public: ...
Symlink's user avatar
  • 481
0 votes
1 answer
80 views

I'm working on some basic object practice and I faced this problem. This class is named Matrix, and I tried to initialize a7 to a2.mutiply(a5). But the result is quite different when I tried to ...
niorix's user avatar
  • 65
2 votes
1 answer
136 views

I came across an unfamiliar move assignment operator signature in Pytorch' tensor backend (ATen, source). Just out of curiosity, what does the && operator do at the end of Tensor & Tensor:...
Jan Ca's user avatar
  • 183
1 vote
1 answer
267 views

As an extension to This question, i am trying to get my move assignment correct. I have the following code: // copy assignment operator LinkedList<T>& operator= (LinkedList<T> other) ...
tannic's user avatar
  • 39
0 votes
0 answers
67 views

Recently, I have stumbled over this pattern: class Foo { Foo() = default; Foo(const Foo &) = default; Foo(Foo &&) = default; Foo & operator=(Foo rhs) { swap(rhs); ...
user666412's user avatar
8 votes
2 answers
820 views

I frequently need to implement C++ wrappers for "raw" resource handles, like file handles, Win32 OS handles and similar. When doing this, I also need to implement move operators, since the default ...
Fredrik Orderud's user avatar
9 votes
3 answers
37k views

Firstly, I really checked if there is a question already been asked but I could not find any. Error message should not deceive you my situation is a bit different I guess or I am just missing ...
eminomur's user avatar
8 votes
3 answers
507 views

It compiles with /permissive but fails with /permissive-. What is not conforming and how to fix it? Why it's fine in (2) but fails in (4)(3)? If I remove operator long it also fine. How to fix it ...
OwnageIsMagic's user avatar
0 votes
0 answers
35 views

The following is a snippet right at the end of this article by Mr. Thomas Becker: X& X::operator=(X&& rhs) { // Perform a cleanup that takes care of at least those parts of the // ...
Ayrosa's user avatar
  • 3,523
0 votes
2 answers
162 views

I am trying to create a move assignment function but I keep get getting "pointer being freed was not allocated" const MyString& MyString::operator=(MyString&& move){ cout<< "...
Dhruv Patel's user avatar
1 vote
3 answers
185 views

I want to overload a common copy-assignment operator normally. At first I used a interface that only requires a const reference to the source, and explicitly disabled the interface that accepts a ...
Leon's user avatar
  • 2,165
5 votes
2 answers
3k views

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::...
alfC's user avatar
  • 16.8k
3 votes
1 answer
108 views

I encountered a problem where gcc compiler moved local variable (not temporary) as rvalue argument to a function. I have a simple example: class A { public: A() {} A& operator=(const A&...
Sasha Itin's user avatar
1 vote
2 answers
264 views

I have two cpp files and one hpp file. Main.cpp, Ab.cpp and Ab.hpp. In these files I have created a class 'Ab' that has a default constructor and a constructor that takes a string. Within the class I ...
Oberruk's user avatar
  • 13
1 vote
1 answer
2k views

Type &Type::operator=(Type &&rhs) { if(this == &rhs) //is there any need of self-assignment . returh *this ; } ... } //since it will be called on r-value so why self-assignment ??
Hamza's user avatar
  • 82
0 votes
1 answer
47 views

I grabbed the following code from Ten C++11 Features Every C++ Developer Should Use. I want to see the output with / without move constructor and move assignment operator. The original code compiles ...
duong_dajgja's user avatar
  • 4,276
1 vote
2 answers
965 views

I’m writing a simple Matrix class, and I’ve defined, among others, an operator+ overloading and a move assignment. It looks like something happens when the two of them interact, but I can’t find where ...
A.J.'s user avatar
  • 103
2 votes
1 answer
81 views

Given the latitude that a C++ compiler has in instantiating temporary objects, and in invoking mechanisms like return value optimization etc., it is not always clear by looking at some code if move or ...
Ziffusion's user avatar
  • 8,973
0 votes
0 answers
29 views

I am learning more about smart pointers in C++14. Consider the following MWC: #include <iostream> #include <string> #include <memory> class House { public: House &operator=(...
Eduardo J. Sanchez's user avatar
2 votes
1 answer
3k views

I require some help in understanding the process of inheritance of move assignment operator. For a given base class class Base { public: /* Constructors and other utilities */ /* ... */ ...
Akhaim's user avatar
  • 152
1 vote
1 answer
267 views

As follow up to: Double free of child object after using the copy constructor I followed the rule of 5 as suggested. But now it seems like the move assignment is happening on an uninitialized object (...
eKKiM's user avatar
  • 441
2 votes
1 answer
202 views

The statement Implicitly-declared move assignment operator If no user-defined move assignment operators are provided for a class type (struct, class, or union), and all of the following is ...
Moia's user avatar
  • 2,394
-1 votes
1 answer
1k views

I have the following code snipet: // code snipet one: #include <memory> #include <iostream> #include <queue> struct A { uint32_t val0 = 0xff; ~A() { std::cout &...
Dongwei Wang's user avatar
-1 votes
2 answers
533 views

I am trying to implement copy and move assignments, but I don't understand how should I use them. I have read the following topic When did copy assignment operator called? But it did not work for me....
Alex Lavriv's user avatar
3 votes
1 answer
3k views

I have been reading the book "The C++ programing language 4th edition" by Bjarne Stroustrup (The creator of c++) and have been learning about move constructors and move assignments. In the book for ...
Amr's user avatar
  • 450
6 votes
1 answer
529 views

I created the following class to understand the behavior of std::sort: class X { public: X(int i) : i_(i) { } X(X&& rhs) noexcept : i_(std::move(rhs.i_)) { mc_++; } X& operator=(X&...
Daniel Langr's user avatar
  • 24.2k