2,435 questions
3
votes
2
answers
204
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 ...
4
votes
1
answer
159
views
Copy constructor with impossible requires-constraint
In the following program, the copy constructor of struct S is declared with the constraint that the class is not copy constructible:
#include <concepts>
template <typename T>
struct S {
...
0
votes
0
answers
72
views
How to process only one member in the copy constructor and assignment operator, leaving others to default processing? [duplicate]
I have one member that requires a deep copy (f2 here) and many other members (f1 here) which I don't want to copy manually in the copy constructor and assignment operator, like in this demo (I decided ...
3
votes
1
answer
118
views
Why copying of vector's elements can be done with not-const lvalue argument?
If one copies one std::vector into another, or copies elements of a std::vector in a larger (reserve) or smaller (shrink_to_fit) block of heap memory, what constructor of the element's type is called?
...
15
votes
1
answer
380
views
Copying of std::vector filled with neither copy constructible nor copy assignable elements
Consider I have a class A that is neither copy constructible nor copy assignable, but has any-like constructor A(auto &&). Can I create copies of std::vector<A> then?
#include <...
4
votes
2
answers
176
views
C++: Best way to strengthen the type safety of assignment to std::function?
Summary: Earlier, I asked this question:
Why does C++ allow std::function assignments to imply an object copy?
Now that I understand why the code works the way it does, I would like to know how to fix ...
4
votes
3
answers
189
views
Copy constructor called when pushing object onto std::vector
#include <iostream>
#include <vector>
class Car{
public:
int weight;
Car(int weight): weight(weight){
};
Car(const Car& other){
std::cout<<&...
0
votes
0
answers
77
views
Disabling an illegal copy-constructor using a requires-clause not working [duplicate]
Why does the following code not compile with GCC while it does compile with Clang?
I know that without the requires-clause the code would be illegal for the instance foo<0> because passing the ...
3
votes
0
answers
81
views
C++: Constructor invocation when initializing from a function returning a reference to *this
I'm trying to understand the exact behavior of object initialization in C++ when using a function that returns a reference to *this. Consider the following generic code:
class T {
public:
T& ...
4
votes
0
answers
166
views
move or copy -- major compilers disagree, who's right?
A class below is a wrapper that either keeps a reference on lvalue or a copy of rvalue. The latter is either copied or moved depending on specialization.
This is a very common problem when using ...
3
votes
1
answer
173
views
Replacing std::is_base_of_v in case of disable_if_same_or_derived
Eric Niebler wrote some time ago an article about Universal References and the Copy Constructor:
Niebler
The solution at the end is
template <typename A, typename B>
using ...
0
votes
1
answer
56
views
Creating a "view" of a sibling class avoiding unnecessary copy
Consider this (simplified) class hierarchy in a c++98 source
(godbolt):
class Shape
{
public:
virtual class CurvilinearShape as_curvilinear_shape() const =0;
};
class CurvilinearShape : public ...
2
votes
2
answers
169
views
GCC does not generate machine code for out-of-class defaulted copy constructor
Assume the following source file (translation unit; TU):
struct X {
int i;
X(const X&);
X(X&&);
};
X::X(const X&) = default;
X::X(X&&) = default
If I compile ...
3
votes
2
answers
213
views
Is there any type defined in Standard Library which has copy constructor but doesn't have a move constructor?
In general, the C++ Standard Library types are designed with both copy and move semantics in mind.
Is there any type defined in the Standard Library which has a copy constructor but doesn't have a ...
3
votes
1
answer
112
views
Object's state changes after its construction and before a member function call
Below program has been reduced as far as possible to show the issue encountered with Visual Studio C++ compiler.
f is some algorithm function taking on input predicate object P p, which has user-...