Skip to main content
Filter by
Sorted by
Tagged with
3 votes
2 answers
230 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,867
4 votes
1 answer
164 views

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 { ...
Fedor's user avatar
  • 24.8k
3 votes
1 answer
119 views

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? ...
Fedor's user avatar
  • 24.8k
15 votes
1 answer
393 views

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 <...
Fedor's user avatar
  • 24.8k
4 votes
2 answers
184 views

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 ...
srm's user avatar
  • 3,361
4 votes
3 answers
207 views

#include <iostream> #include <vector> class Car{ public: int weight; Car(int weight): weight(weight){ }; Car(const Car& other){ std::cout<<&...
simply lemon's user avatar
0 votes
0 answers
79 views

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 ...
Orbit's user avatar
  • 23
3 votes
0 answers
88 views

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& ...
michael3.14's user avatar
4 votes
0 answers
171 views

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 ...
Gene's user avatar
  • 754
3 votes
1 answer
204 views

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 ...
Juergen's user avatar
  • 3,769
0 votes
1 answer
56 views

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 ...
MatG's user avatar
  • 796
2 votes
2 answers
174 views

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 ...
Daniel Langr's user avatar
  • 24.2k
3 votes
2 answers
214 views

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 ...
John's user avatar
  • 3,574
3 votes
1 answer
112 views

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-...
Fedor's user avatar
  • 24.8k
0 votes
3 answers
180 views

The following code would not compile: #include <utility> class Foo{ public: Foo(const Foo& foo) = delete; explicit Foo(Foo&& foo) = default; Foo() = default; Foo ...
ttzytt's user avatar
  • 101
0 votes
0 answers
42 views

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 ...
Tippa Toppa's user avatar
0 votes
2 answers
102 views

class Test{ int num; const int constVal; int& ref; public: Test(int a):constVal(2),ref(a){ std::cout<<"create"<<std::endl; } }; int main() { ...
Hee's user avatar
  • 9
3 votes
1 answer
173 views

struct X { X() = default; X(const X& src) { cout << "copy" << endl; } }; int main() { X x1; X x2(move(x1)); } Output: copy struct ...
Andrey Rubliov's user avatar
0 votes
1 answer
109 views

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 << "...
lucottoDA's user avatar
1 vote
0 answers
49 views

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 ...
zzzhhh's user avatar
  • 73
0 votes
0 answers
86 views

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 ...
John's user avatar
  • 3,574
2 votes
1 answer
151 views

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]) { } ...
Andrey Rubliov's user avatar
2 votes
3 answers
116 views

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 {...
Fatemeh Rostamipour's user avatar
0 votes
1 answer
88 views

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 ...
M.B.'s user avatar
  • 279
1 vote
0 answers
67 views

I have the following example from a course, about Deep Copying: #include <iostream> class DeepCopy { private: int *_myInt; public: DeepCopy(int val) { _myInt = (int *)...
rebecca73's user avatar
0 votes
2 answers
210 views

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 ...
ValeriyKarasikov's user avatar
1 vote
1 answer
535 views

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 ...
Ronnie Shipman's user avatar
0 votes
0 answers
67 views

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 ...
Robert Deibel's user avatar
0 votes
1 answer
134 views

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 ...
Sourabh's user avatar
  • 787
2 votes
1 answer
167 views

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 )...
shuhalo's user avatar
  • 6,562
0 votes
1 answer
136 views

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 ...
bytecode77's user avatar
0 votes
0 answers
69 views

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 ...
一碗给力嗯's user avatar
-1 votes
1 answer
118 views

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 ...
蔡天意's user avatar
3 votes
2 answers
143 views

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)) { ...
notfynnaf's user avatar
1 vote
1 answer
104 views

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 ...
Fedor's user avatar
  • 24.8k
0 votes
1 answer
245 views

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 ...
meetanandkr's user avatar
2 votes
1 answer
108 views

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 ...
Xhyub's user avatar
  • 31
0 votes
0 answers
99 views

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: ...
DR4NKR1D3R's user avatar
0 votes
1 answer
147 views

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 ...
DWil's user avatar
  • 35
2 votes
0 answers
83 views

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] = ...
Samvel Muqelyan's user avatar
1 vote
1 answer
178 views

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 ...
qwert789812's user avatar
0 votes
0 answers
86 views

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 ...
Tharani B's user avatar
-5 votes
2 answers
133 views

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 ...
CREATIVITY Unleashed's user avatar
-1 votes
1 answer
97 views

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 ...
TobyFromHR's user avatar
0 votes
1 answer
161 views

Given the next code: #include <iostream> #include <vector> #include <cstring> class Person { public: Person(const char *i_Name = "Unknown", int i_Age = 0) : ...
Yarin0600's user avatar
-1 votes
1 answer
46 views

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 ...
El ayoub's user avatar
4 votes
1 answer
223 views

For below code, is there any reason compiler will choose non-explicit constructor.. struct S { S() = default; explicit S(S & cp) { std::cout << "explicit" << ...
Kishan Parmar's user avatar
0 votes
0 answers
51 views

I would like to understand when a copy constructor or assignment is called. Assume the following: class Foo { public: Foo() { cout << "foo constructor " << this <&...
Sam's user avatar
  • 1
6 votes
3 answers
308 views

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; } ...
user avatar
2 votes
0 answers
132 views

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 &...
tommsch's user avatar
  • 778

1
2 3 4 5
49