155 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
2
votes
1
answer
157
views
std::copy_assignable_v not working for reference types, how to fix?
Consider the following code where I test whether a type is copy-assignable by using the std::is_copy_assignable_v type-trait:
#include <print>
#include <type_traits>
struct IntProxy
{
...
0
votes
2
answers
236
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
230
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 ...
0
votes
0
answers
102
views
copy assignment without swap
I'm trying to understand copy assignment in C++. I know it's best to use copy-and-swap idiom for this operator. But for this simple class without consider the Exception safety, when will *ps = *(h.ps);...
6
votes
2
answers
253
views
Modern ways to implement assignment for value types
This question is about how to implement assignment in modern C++ value types, with the goal of avoiding misleading code or code that is inconsistent with built-in behavior.
In C++, generated values (...
0
votes
2
answers
103
views
why default copy constructor can copy const or reference member but default copy assign operator can not?
class Test{
int num;
const int constVal;
int& ref;
public:
Test(int a):constVal(2),ref(a){
std::cout<<"create"<<std::endl;
}
};
int main() {
...
1
vote
1
answer
101
views
Elegant way to mix Move constructor with Base class's Copy assignment operator
Problem Explanation
Here's what I understood so far during last two month studying about Move semantics:
Move constructor implicitly insert SomeClass::operator=(const SomeClass&) = delete at end ...
0
votes
0
answers
64
views
Where to place virtual base class copy assignment operator in derived class copy assignment operator?
In an old closed-source codebase I came across a comment where the author suggests placing virtual base class copy assignment after those of immediate base classes or that it can result in "...
-1
votes
1
answer
93
views
C++ Assignment Operator overloading unexpected behavior [duplicate]
I am new to C++ and trying to learn it the proper way. In particular, I am practicing a simple assignment operator overloading in the following class
class Point {
public:
int x;
int y;
...
0
votes
1
answer
49
views
Python variable assignment to avoid sharing the same memory space [duplicate]
I need some help eith the following code in Python, is an implementation of an ALNS heuristic for vrp that I'm trying to create.
def run_heuristic(num_customers, num_vehicles, num_iterations, ...
1
vote
2
answers
264
views
What's the correct code for the move assignment in Bjarne's example Vector?
I am reading Chapter 5 of the 2nd edition of A Tour of C++ by Bjarne Stroustrup. He is using an example implementation of Vector to convey his ideas and gets to the move constructor and shows the code ...
3
votes
1
answer
214
views
Why std::string a; std::string b; a + b = "abc"; OK? [duplicate]
#include <iostream>
#include <string>
int main()
{
std::string a;
std::string b;
a + b = "dadas";
}
PS D:\WeCode\local_c++> & 'c:\Users\z00841303.vscode\...
4
votes
1
answer
144
views
Why is assigning a container's element to the container (not) a well-defined C++?
In C++ there is the infamous problem of self-assignment: when implementing operator=(const T &other), one has to be careful of the this == &other case to not destroy this's data before copying ...
0
votes
0
answers
51
views
In a chaining call, is copy assignment called? [duplicate]
I would like to understand when a copy constructor or assignment is called. Assume the following:
class Foo {
public:
Foo()
{
cout << "foo constructor " << this <&...
0
votes
0
answers
58
views
Confusion on template copy assignment function
[First of First: Vs2019 on Windows10, only C++11 supported]
I've got confused on template copy assignment function like:
enter image description here
I found the specilization version is not working, ...