2,434 questions
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 ...
4
votes
1
answer
164
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 {
...
3
votes
1
answer
119
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
393
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
184
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
207
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
79
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
88
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
171
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
204
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
174
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
214
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-...
0
votes
3
answers
180
views
Returning an object with only explicit move constructor
The following code would not compile:
#include <utility>
class Foo{
public:
Foo(const Foo& foo) = delete;
explicit Foo(Foo&& foo) = default;
Foo() = default;
Foo ...
0
votes
0
answers
42
views
How to use only the move constructor and the move assignment operator [duplicate]
Please tell me how I can correct this piece of code so that the cookie_name_t class does not use a copy constructor and an assignment constructor.
When I do the following:
cookie_name(cookie_name ...
0
votes
2
answers
102
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() {
...
3
votes
1
answer
173
views
If I do not declare move constructor, the copy one is called, but if I delete the move constructor - compilation error - why?
struct X
{
X() = default;
X(const X& src)
{
cout << "copy" << endl;
}
};
int main()
{
X x1;
X x2(move(x1));
}
Output:
copy
struct ...
0
votes
1
answer
109
views
Can't understand why copy constructor gets invoked
The following should be a MRE:
#include <iostream>
class Vector {
public:
double x, y, z;
// Default constructor
Vector() : x(0), y(0), z(0) {
std::cout << "...
1
vote
0
answers
49
views
Why is implicit conversion not allowed in the copy form of initialization when I do not specify explicit? [duplicate]
In C++, I know we cannot use an explicit constructor with the copy form of initialization (with an =) when implicit conversion happens. In other words, we can use the copy form of initialization if ...
0
votes
0
answers
86
views
Is it legal to define a copy constructor for the pure abstract class?
Background:
I intend to use builder pattern to create a branch of concreate classes whose parent classes are the same. Since all the said concreate classes to be instantiated all need a same setting ...
2
votes
1
answer
151
views
How to know if compiler will use copy elision and if I need to use std::move [duplicate]
How can I trust the compiler for non-guaranteed copy elision (in case of return value optimization) in this example code:
struct X
{
X() : size(10000), very_large_buffer(new char[size])
{
}
...
2
votes
3
answers
116
views
I can not instantiate my Student class in Program.cs
I have a Student class in my console application like this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{...
0
votes
1
answer
88
views
Uncertain behaviour of copy constructor in C++ [duplicate]
As a student , I was testing out various ways of invoking the copy constructor . I came across 3 of them :
Test t2 = t1 // 1 ;
Test t3(t1) // 2 ;
Test t4 ; // 3 ;
t4 = t1 ;
The explicitly defined ...
1
vote
0
answers
67
views
Assignment operator overloading is being bypassed when trying to copy a class instance [duplicate]
I have the following example from a course, about Deep Copying:
#include <iostream>
class DeepCopy
{
private:
int *_myInt;
public:
DeepCopy(int val)
{
_myInt = (int *)...
0
votes
2
answers
210
views
Why can't the constructor be called explicitly?
I was looking into creating a constructor to initialize an encapsulated std::array and came across a problem that the constructor of a copyable type (class A) could not be called explicitly. There are ...
1
vote
1
answer
535
views
Why can't std::variant types be classes with an explicit copy constructor?
If I create a std::variant using classes for the types, and add an explicit copy constructor to one of those classes, the compiler complains. With the default or a non-explicit copy constructor it ...
0
votes
0
answers
67
views
Initialize a struct with a const default through copy
I was trying to define a AbstractOptions struct that would hold some options that are used later on for a different class Operator with a concrete implementation ImplementedOptions. As a default value ...
0
votes
1
answer
134
views
using memcpy for copy constructor and assignment operator for 2d array
I have 2 classes.
Pointer to an array of fixed width and Pointer to a Pointer for implementing 2d array of floats.
I wanted to understand, if and how can I use memcpy for my Copy constructor and ...
2
votes
1
answer
167
views
What about self-construction in C++: should copy and move constructors handle calls with `*this` correctly?
What are best practices in C++ when the copy/move constructors are called with the object itself?
For example:
#include <iostream>
using namespace std;
struct Foo{
Foo( const Foo& foo )...
0
votes
1
answer
136
views
Is it okay to move around references to avoid heavy copy constructor calls?
Let's say, we want to reverse an array, like in this function.
For each swapping of two elements, the copy constructor, destructor and two copy assignments are made.
template<class T>
void ...
0
votes
0
answers
69
views
Why the copy constructor didn't work when the function returned? [duplicate]
I'm new to C++, I'm confused of the copy constructor.
When I run my code below, it only shows me constructor and destructor without copy-constructor.
But when the function foo_bar() returns a object ...
-1
votes
1
answer
118
views
Different ways of constructing a object using another object [duplicate]
A few days ago I read this article: Constructor of type int
But I think I have to clarify something first.
Suppose I have a class Myclass and I already created an object obj1.
Are there differences ...
3
votes
2
answers
143
views
Lifetime issue instance of class || How to build my copy constructor?
My problem lies in the creation of instances of a class in my project.
This is how I create my instances and try to store them:
for (const auto & entry : fs::directory_iterator(path))
{
...
1
vote
1
answer
104
views
Ambiguity in constructing a class from a child class
This question was inspired by What is wrong with the inheritance hierarchy in my example?
Consider we have a struct B, which can be constructed from const reference to another struct A. Can B be ...
0
votes
1
answer
245
views
Deleting copy/move constructor for singleton class in cpp
The below code snip from a singleton class(claiming to be!!) is used for returning the singleton instance of the class using reference. The copy/move constructors were not marked as deleted in this ...
2
votes
1
answer
108
views
Copying objects between pointers and references C++
I have done some reading starting on the Rule of three and this has given me some exposure to memory management when writing C++ and it is challenging coming from a Java background.
I'm just writing ...
0
votes
0
answers
99
views
segmentation fault (can solve with initialization list, can't with copy constructor)
I'm having segmentation fault error in my code:
#include <SFML/Graphics.hpp>
#include "game.hpp"
#include "map.hpp"
#include "button.hpp"
class Game {
private:
...
0
votes
1
answer
147
views
How to write copy/move constructors with delegated constructors and conditional initialiser lists
I'm close to finishing my container but my last problem to solve is how to handle the copy/move constructor and appropriately construct the correct member variable inside the private union member ...
2
votes
0
answers
83
views
Problem in defining of Vector class Copy Constructor (C++)
I tried to define a Vector class and some functions of it manually. Everything seemed like okay, until I wrote the copy constructor, where I get a warning in this part of the loop:
this->arr[i] = ...
1
vote
1
answer
178
views
c++ when do vector push_back deep copy objects?
I created a vector and used push_back to put several node objects into it. However, I can't predict when its going to use the move constructor or copy constructor.
Is there any pattern to when ...
0
votes
0
answers
86
views
Boost Bimap with unique_ptr
I am trying to insert unique_ptr into the boost::bimap, but i am getting the error "call to implicitly deleted copy constructor". I am moving the unique_ptr through std::move and i have move ...
-5
votes
2
answers
133
views
Does bitwise copy constructor really share same memory location for objects? [closed]
Hey I am confused regarding memory allocation during bitwise copy of objects in C++.
Here's what the book by Herbert Schildt C++ fourth edition Pg:366 says:
Let's begin by restating the problem that ...
-1
votes
1
answer
97
views
Trying to figure out a Copy Constructor for the course class
This is the course class in question.
class Course {
//** You may not change the declarations in this private: area.
CourseName name; ///< Name of the course
int ...
0
votes
1
answer
161
views
Understanding Copy Constructor and Temporary Object Creation in C++ When Passing Objects by Value
Given the next code:
#include <iostream>
#include <vector>
#include <cstring>
class Person
{
public:
Person(const char *i_Name = "Unknown", int i_Age = 0)
: ...
-1
votes
1
answer
46
views
copy static array in a dynamic array in c++
The main goal of this code is to copy part of the strings from myArray to neuArray and then output the copied strings from neuArray to the console.
the code is working but not 100% because i get the ...
4
votes
1
answer
223
views
Why compiler always choose non explicit constructor for copy-list-initialization?
For below code, is there any reason compiler will choose non-explicit constructor..
struct S {
S() = default;
explicit S(S & cp) {
std::cout << "explicit" << ...
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 <&...
6
votes
3
answers
308
views
Copy semantics and vectors
I am dealing with objects that allocate memory for internal use. Currently, they are not copyable.
E.g.
class MyClass
{
public:
MyClass() { Store = new int; }
~MyClass() { delete Store; }
...
2
votes
0
answers
132
views
Why - in that example with an extended lambda - is an ambigious copy constructor and some deleted function
I do not understand the behaviour of the following code:
template< bool b >
struct Foo {
Foo() = default;
__host__ Foo( const Foo & ) requires( b ) {}
__device__ Foo( const Foo &...