75 questions
0
votes
2
answers
234
views
Why is copy-and-swap preferred over self-assignment check? [closed]
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 ...
3
votes
2
answers
219
views
What is the rationale behind the C++ compiler’s rules for implicitly declaring special member functions?
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 ...
2
votes
1
answer
188
views
Why can't a struct with a reference member have default move assignment operator?
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;
...
0
votes
1
answer
113
views
Does the compiler generate the move operations (not default) in case of a default destructor? [duplicate]
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?
...
0
votes
0
answers
86
views
Is there a caveat for using placement new instead of move assignment for a emplace_back imitation on a static array
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....
1
vote
0
answers
17
views
What is the causing my buffer to overrun?
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 ...
0
votes
0
answers
114
views
Does the Copy and Swap idiom have a not-required swap?
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 ...
0
votes
1
answer
142
views
The move semantics and time complexity of nested std::vector<std::vector<std::string>> rvalue assignment
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::...
0
votes
0
answers
166
views
Identification of automatically generated move assignment operator
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>...
1
vote
1
answer
89
views
C++ implicitly declared move assignment operator calling implicitly declared copy assignment operator of base class
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? ...
1
vote
3
answers
245
views
Can I reliably emplace_back in a vector of a type that does not have an assignment operator?
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 ...
4
votes
1
answer
1k
views
When is `noexcept` required on move assignment?
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(...
1
vote
1
answer
2k
views
Explicitly defaulted copy/move assignment operators implicitly deleted because field has no copy/move operators. C++ [duplicate]
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 ...
0
votes
0
answers
201
views
AUTOSAR C++ Rule 6-2-1 - Move and Copy Assignment Operators
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 ...
6
votes
1
answer
672
views
std::vector move assignment vs move construction: why is the state of 'other' not consistent?
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 ...
1
vote
1
answer
173
views
Problem with my Copy and Move constructor and assignment operators
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 ...
1
vote
1
answer
163
views
Ambiguous overload for ‘operator=’ when trying to invoke the move assignment operator [duplicate]
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 ...
0
votes
0
answers
236
views
How to write move constructor and move assignment operator for dynamic array or uni_ptr?
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 ...
5
votes
2
answers
180
views
Why is copy assigment possible, if a class has only a (templated) move assignment operator?
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>
...
2
votes
0
answers
1k
views
Cannot assign a reference that has a deleted copy constructor?
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, ...
2
votes
1
answer
294
views
std::is_move_assignable_v false though there is a move assignment operator
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 (...
7
votes
1
answer
481
views
Why in C++11 or C++14 does the compiler implicitly delete the copy constructor when I declare a move assignment operator?
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 ...
1
vote
1
answer
132
views
C++: question about move constructor/assignment using Stroustrup example
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 ...
0
votes
1
answer
70
views
class to class type conversion through constructor method output is 5500 why not 5555
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(...
0
votes
1
answer
206
views
Move constructor and move assignment operator throw error when set to default in .cc source file
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:
...
0
votes
1
answer
80
views
The difference between initialize a object and assign to an object
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 ...
2
votes
1
answer
136
views
Strange Move Assignment Operator Signature [duplicate]
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:...
1
vote
1
answer
267
views
Move constructor vs. Move assignment
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) ...
0
votes
0
answers
67
views
C++ assignment by value, what are the drawbacks?
Recently, I have stumbled over this pattern:
class Foo {
Foo() = default;
Foo(const Foo &) = default;
Foo(Foo &&) = default;
Foo & operator=(Foo rhs) {
swap(rhs);
...
8
votes
2
answers
820
views
Is it legal to implement assignment operators as "destroy + construct"?
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 ...
9
votes
3
answers
37k
views
free(): double free detected in tcache 2 in C++
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 ...
8
votes
3
answers
507
views
msvc /permissive- std::string overloaded operator '=' is ambiguous
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 ...
0
votes
0
answers
35
views
What does the author mean by "leave the object in an assignable state"? [duplicate]
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
// ...
0
votes
2
answers
162
views
Creating a move assignment function, keep getting "pointer being freed was not allocated"
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<< "...
1
vote
3
answers
185
views
Why GCC refuses a const reference within a copy-assignment operation?
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 ...
5
votes
2
answers
3k
views
Allocator propagation policies in your new modern C++ containers
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::...
3
votes
1
answer
108
views
why gcc 6.4.0 c++14 moves automatically lvalue to rvalue
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&...
1
vote
2
answers
264
views
Overloading "*" operator for a class to return class variable
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 ...
1
vote
1
answer
2k
views
Is a self-assignment check really required while implementing move assignment operator for a class? [duplicate]
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 ??
0
votes
1
answer
47
views
Commenting out `move constructor` and `move assignment operator` makes compilation error
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 ...
1
vote
2
answers
965
views
Problem with move assignment in C++. Illegal instruction: 4
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 ...
2
votes
1
answer
81
views
In C++, how can one predict if move or copy semantics would be invoked?
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 ...
0
votes
0
answers
29
views
Fixing assignment of an object's pointer members via smart pointers
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=(...
2
votes
1
answer
3k
views
Inheritence of the move assignment operator in C++
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 */
/* ... */
...
1
vote
1
answer
267
views
C++ move assignment to uninitialized object?
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 (...
2
votes
1
answer
202
views
What does it means user-declared for Implicitly-declared move assignment operator?
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 ...
-1
votes
1
answer
1k
views
How to correctly transfer the ownership of a shared_ptr?
I have the following code snipet:
// code snipet one:
#include <memory>
#include <iostream>
#include <queue>
struct A {
uint32_t val0 = 0xff;
~A() {
std::cout &...
-1
votes
2
answers
533
views
c++ copy assignment and move assignment are not being called
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....
3
votes
1
answer
3k
views
Move Constructor & Move Assignment
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 ...
6
votes
1
answer
529
views
Why std::sort construct objects? [duplicate]
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&...