436 questions
1
vote
2
answers
179
views
Why can't this friend class access the private move constructor?
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>);
...
3
votes
2
answers
219
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 ...
15
votes
1
answer
744
views
Is implicit conversion to std::optional guaranteed to use move constructor?
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 ...
6
votes
1
answer
206
views
Can move constructor with (const T&&) parameter be defaulted?
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 ...
0
votes
1
answer
113
views
Does the compiler generate the move operations (not default) in case of a default destructor? [duplicate]
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?
...
1
vote
1
answer
92
views
`auto x = nonmoveable{};` rejected by MSVC but allowed by GCC and clang
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 ...
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& ...
2
votes
1
answer
96
views
Why does moving a const ref return value into another function still move-construct the object?
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 ...
0
votes
1
answer
96
views
std::string and default move constructor causing heap corruption
While refactoring some code, I've run into an issue with heap corruption.
namespace GPU
{
struct ShaderStage
{
public:
ShaderStage(ShaderStageType, const Path&);
...
1
vote
1
answer
139
views
Why is the move constructor is not being called?
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 ("...
4
votes
0
answers
168
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 ...
2
votes
2
answers
173
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 ...
0
votes
3
answers
177
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
0
answers
114
views
Does the Copy and Swap idiom have a not-required swap?
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 ...
0
votes
0
answers
60
views
CPP Help: MOVE CTOR NOT getting hit [duplicate]
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>
...
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 ...
2
votes
1
answer
148
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
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
0
answers
144
views
How to pass derived object as rvalue reference into base class pointer members using move constructor?
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. ...
0
votes
0
answers
46
views
Please explain move constructors and assignement logic flow [duplicate]
`
#include <iostream>
#include <vector>
class Move {
private:
int* data;
public:
Move(int d) {
data = new int;
*data = d;
std::cout << "...
0
votes
0
answers
114
views
problem with moving vector of array in c++23 on gcc 13.2
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 ...
2
votes
1
answer
92
views
Cast to rvalue reference prevents copy elision
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 ...
0
votes
1
answer
143
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 ...
1
vote
1
answer
311
views
Why does bind_front/bind_back/not_fn/bind require Args... to be move-constructible?
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-...
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 ...
1
vote
1
answer
160
views
Coding std::sort on struct that needs deep copy
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(); ...
0
votes
0
answers
29
views
Text provides a code which should call the move constructor. However, it is not getting called. Where the implementation has gone wrong? [duplicate]
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;...
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, ...
5
votes
2
answers
197
views
Weird behavior when using std::move shared_ptr with conditional operator
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()...
0
votes
1
answer
116
views
How to make rvalue behave like lvalue reference in c++?
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;
...
-1
votes
1
answer
629
views
How to perfectly forward a universal reference that is either a const ref or a movable rvalue?
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 ...
-2
votes
1
answer
252
views
User-declared destructor doesn't delete implicitly-declared move constructor (and co) [duplicate]
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-...
1
vote
1
answer
60
views
Can class with const members be assigned or copied
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 ...
0
votes
0
answers
73
views
why is copy constructor called for every element of vector when i use push_back? [duplicate]
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, ...
0
votes
1
answer
113
views
Can you design a constructor to allow `Class c(std::move(another_class))` when the class has a member of type const std::unique_ptr?
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 ...
1
vote
3
answers
245
views
Can I reliably emplace_back in a vector of a type that does not have an assignment operator?
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 ...
0
votes
1
answer
172
views
Is a constructor a move constructor if the parameter is not an object of the class?
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)),
...
0
votes
0
answers
198
views
C++ Eigen Is there a move constructor of Eigen::Vector3d class and how to call it?
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)...
0
votes
0
answers
115
views
Constructing elements of union
The following class outputs when a constructor is called:
class A {
public:
A() {
std::cout << "Default Constructor called at address:" << this << "!\n&...
0
votes
0
answers
81
views
Using move constructor as substitute for default constructor
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 ...
0
votes
1
answer
178
views
C++ copy and move constructors with lambda expressions
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 ...
1
vote
1
answer
582
views
C++ Move constructor for object with std::vector and std::array members
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 ...
1
vote
0
answers
274
views
Strange behaviour of emplace_back() function of std::vector<> container
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>...
1
vote
0
answers
75
views
Preventing preservation of constness of captured variables in C++ lambdas capturing by value [duplicate]
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> ...
0
votes
0
answers
96
views
Why is the copy constructor called twice here? [duplicate]
My code is this:
#include <vector>
#include <iostream>
class A{
public:
A() = default;
A(const A& obj){
std::cout << "Copy c-tor&...
0
votes
0
answers
61
views
Why the move constructor isn't invoked? [duplicate]
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{
...
5
votes
1
answer
224
views
Is it meaningful\suitable to mark the derived class as movable while the base class is non-moveable?
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 ...
0
votes
1
answer
921
views
Constructing a shared_ptr in a for loop and move assignment
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::...