Skip to main content
Filter by
Sorted by
Tagged with
1 vote
2 answers
179 views

I get an error at the static assert: #include <type_traits> template <typename element_t> class MyVector { public: static_assert(std::is_move_constructible_v<element_t>); ...
Zebrafish's user avatar
  • 16.3k
3 votes
2 answers
219 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,857
15 votes
1 answer
744 views

The following code block illustrates a difference between returning a std::optional via an implicit conversion (i.e. fn) vs. an explicit construction (i.e. fn2). Specifically, the implicit conversion ...
MarkB's user avatar
  • 2,230
6 votes
1 answer
206 views

I see a similar question Default move constructor taking a const parameter, which is 8 years old, with the answer No. But at the same time, a slightly modified program with the constructor defaulted ...
Fedor's user avatar
  • 24.7k
0 votes
1 answer
113 views

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? ...
Ali Sedighi's user avatar
1 vote
1 answer
92 views

I am trying to move from T t{ ... }; construction to auto t = T{ ... }; in order to make it consistent with function calls like auto p = std::make_unique<T>(...);. I am however running into ...
Dominik Kaszewski's user avatar
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
2 votes
1 answer
96 views

In the following example the function Table::get_subtable() is returning a const reference to an internal subobject (SubTable). This value is then std::moved and passed into a static factory function ...
glades's user avatar
  • 5,374
0 votes
1 answer
96 views

While refactoring some code, I've run into an issue with heap corruption. namespace GPU { struct ShaderStage { public: ShaderStage(ShaderStageType, const Path&); ...
garlfin's user avatar
  • 11
1 vote
1 answer
139 views

Below is my C++ code. I was expecting my move ctor to be called here in my main: Mystring larry2 = "some larry"; main.cpp: int main() { //Mystring no_name; Mystring larry ("...
gaurav s's user avatar
4 votes
0 answers
168 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
2 votes
2 answers
173 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
0 votes
3 answers
177 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
0 answers
114 views

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 ...
CS Student's user avatar
0 votes
0 answers
60 views

I'd like to ask for your help. I am new in this and I can't get to make MOVE ctor work. My move CTOR is not getting hit for some reason, I don't understand. Please help me. #include<iostream> ...
X-Maki's user avatar
  • 41
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
2 votes
1 answer
148 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
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
0 answers
144 views

I implemented composite design pattern for calculating the nested mathematical expressions modeled in a binary tree. I need to hold two base class pointers *left,*right in my derived composite class. ...
hamid's user avatar
  • 19
0 votes
0 answers
46 views

` #include <iostream> #include <vector> class Move { private: int* data; public: Move(int d) { data = new int; *data = d; std::cout << "...
alkendev's user avatar
0 votes
0 answers
114 views

I am getting a problem with my code. https://godbolt.org/z/9hdWrdPnG. Its a normal Trie implementation where I have used std::array to store indices of nodes into a std::vector which increases as new ...
DeltaVega's user avatar
  • 101
2 votes
1 answer
92 views

I read more related articles, but none of the answers clarified my doubts. Why in the code below the optimization takes place only when there is no cast to an rvalue reference, otherwise either the ...
pauk's user avatar
  • 408
0 votes
1 answer
143 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
1 vote
1 answer
311 views

I noticed that the std::bind_front/std::bind_back/std::not_fn that yields the perfect forwarding call wrapper all require that the function argument and argument arguments passed in must be move-...
康桓瑋's user avatar
  • 46.8k
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
1 vote
1 answer
160 views

I have a struct typedef unsigned int gsk_uint32; typedef struct _gsk_oid { int count; gsk_uint32 * elements; } gsk_oid; struct oidX : public gsk_oid { oidX(); ...
Charles's user avatar
  • 519
0 votes
0 answers
29 views

As I was going through the C++ book by Siddhartha Rao, I came across the following code (pardon the lack of proper spacing): #include <iostream> #include <algorithm> using namespace std;...
paapi_420's user avatar
0 votes
0 answers
58 views

[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, ...
Louis's user avatar
  • 1
5 votes
2 answers
197 views

I was working on some C++ code using std::move on shared_ptr and got really weird output. I've simplified my code as below int func(std::shared_ptr<int>&& a) { return 0; } int main()...
danry's user avatar
  • 89
0 votes
1 answer
116 views

I am currently writing a simple algebra library in C++. The library has a Matrix class defined as follows: template<typename T> class Matrix { private: size_t n, m; T **const tab; ...
yomol777's user avatar
  • 473
-1 votes
1 answer
629 views

I have coded a lock-free and thread-safe ring queue with C++20, and it works so far. The only thing is not perfect that it has to have two enque() methods, one accepts a const reference to a lvalue as ...
Leon's user avatar
  • 2,165
-2 votes
1 answer
252 views

I'm having trouble understanding why my declaring a destructor in my class doesn't delete the implicitly declared move constructor as is specified in this documentation, where it says : If no user-...
Getter's user avatar
  • 955
1 vote
1 answer
60 views

According to abseil.io/tips/177, it said Specifically, if your class has const members, it cannot be assigned to (whether by copy-assignment or move-assignment). The language understands this: if ...
Biear's user avatar
  • 53
0 votes
0 answers
73 views

i'm working on this exercise where i implement a class that holds a dynamically allocated built-in array (int*). i'm trying to implement the big 5 and i think it's working (it compiles and runs, ...
Russell Butler's user avatar
0 votes
1 answer
113 views

Below is the simplified version of a class that I am trying to design class Class { public: Class(std::unique_ptr<int>&& ptr): ptr_(std::move(ptr)) {} private: // works only ...
mercury0114's user avatar
  • 1,499
1 vote
3 answers
245 views

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 ...
really's user avatar
  • 123
0 votes
1 answer
172 views

Does the && and in the parameters mean that this is a move constructor? Vertex(int&& val, float&& dis) : value_(std::move(val)), distance_(std::move(dis)), ...
Eggy's user avatar
  • 13
0 votes
0 answers
198 views

I suppose the Vector3d class of Eigen have the move constructor, so when I construct an object, my code is listed as below: struct Node{ Node(std::vector<Eigen::Vector3d>&& set_means)...
peng's user avatar
  • 1
0 votes
0 answers
115 views

The following class outputs when a constructor is called: class A { public: A() { std::cout << "Default Constructor called at address:" << this << "!\n&...
mbang's user avatar
  • 865
0 votes
0 answers
81 views

The Question I have a type T that is move assignable and move constructible but not default constructible. I want to create a T temp[N]; using the move constructor so that I can safely move assign Ts ...
mbang's user avatar
  • 865
0 votes
1 answer
178 views

I've bumped into something strange with C++ copy and move constructors, here when passing to the lambda expression both the copy and move constructors get executed. Strangely though, when I change the ...
David Carpenter's user avatar
1 vote
1 answer
582 views

I'm currently implementing a Vector class that is supposed to handle the math. This class has two members std::vector<double> vector_ and std::array<std::size_t, 2> size_. Now I want to ...
fakl's user avatar
  • 127
1 vote
0 answers
274 views

I'm investigating how the emplace_back() function actually works under the hood. Here's the code I'm using for my experiment: #include <iostream> #include <string> #include <optional>...
Ahmed R's user avatar
  • 31
1 vote
0 answers
75 views

Consider the following case: the second static_assert fails (https://gcc.godbolt.org/z/KP4zxvY7G). #include <cstdio> #include <type_traits> #include <vector> std::vector<int> ...
Jean-Michaël Celerier's user avatar
0 votes
0 answers
96 views

My code is this: #include <vector> #include <iostream> class A{ public: A() = default; A(const A& obj){ std::cout << "Copy c-tor&...
Amir reza Riahi's user avatar
0 votes
0 answers
61 views

I defined a class with both copy and move constructors. The copy constructor seems to work fine but when I try to invoke the move constructor it doesn't work. #include <iostream> class A{ ...
Amir reza Riahi's user avatar
5 votes
1 answer
224 views

Is it meaningful\suitable to mark the derived class as movable while the base class is non-moveable? I know this kind of inconsistency is legal in C++, but is it meaningful\suitable in practise? In ...
John's user avatar
  • 3,574
0 votes
1 answer
921 views

I'm trying to get my head around shared pointers at the moment and how they work. I would be really grateful for any advice you could give on the below. Please could you advise on: When to use std::...
purecobalt's user avatar

1
2 3 4 5
9