436 questions
1
vote
2
answers
156
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
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 ...
13
votes
1
answer
717
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
199
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
109
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
89
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
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& ...
2
votes
1
answer
91
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
85
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
137
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
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 ...
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 ...
0
votes
3
answers
173
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 ...