3 ms·
The website sometimes fudges things with somewhat-incorrect statements. Maybe it's a minor issue, but I noticed that it says, for example: > When an initialize
by einpoklum 4y ago
The website sometimes fudges things with somewhat-incorrect statements. Maybe it's a minor issue, but I noticed that it says, for example:
> When an initializer is provided after an equals sign, this is called copy
> initialization. Copy initialization was inherited from the C language.
>
> int width = 5; // copy initialization of value 5 into variable width
>
That's not quite right. Consider the following program:
#include <iostream>
struct A {
A (const A& other) : x{other.x} { std::cout << "Copy ctor\n" ;}
A (A&& other) : x{other.x} { std::cout << "Move ctor\n" ;}
A() : x{0} { std::cout << "Default ctor\n" ;}
A(int x_) : x{x_} { std::cout << "int ctor\n" ;}
A& operator=(const A& other) { x = other.x; std::cout << "Copy assignment\n" ; return *this; }
A& operator=(A&& other) { x = other.x; std::cout << "Move assignment\n" ; return *this; }
int x;
};
int main() { A a1 = A(5); }
The constructor used here will not be the copy constructor. There will be just a single construction, of a1, and it will be the "int ctor", i.e. `A(int x_)`.
See it on GodBolt : https://godbolt.org/z/eT8soWMxE https://godbolt.org/z/eT8soWMxE
I don't know if this characterizes other parts of LearnCPP, haven't gone through it myself.
- thom 4y agoIf you really want to embrace the horror: https://www.youtube.com/watch?v=tH0Z2OvHAd8 https://www.youtube.com/watch?v=tH0Z2OvHAd8
- jdrek1 4y agoCopy initialization does not mean that the copy constructor is called. It is the correct term for an expression `T a = ...;`, which can often result in the copy constructor being called but does not have to. In this case it's mandatory copy elision from a prvalue, which leads to only one constructor being called.
- einpoklum 4y agoYou are absolutely correct. It turns out "copy initialization is the formal term used. It's kind of stupid IMHO, since it may not be initialization by copying or using a copy of anything, but: https://en.cppreference.com/w/cpp/language/copy_initialization https://en.cppreference.com/w/cpp/language/copy_initializati... So I half-take-back my comment.