copy-constructor

Why is there no need to mark the move constructor of a type with a deleted copy constructor as deleted?

老子叫甜甜 提交于 2020-06-23 11:06:57
问题 Consider std::mutex . I understand why std::mutex should not be movable. But its copy constructor is clearly marked as deleted, but I have not seen such a declaration for its move constructor. So why does cppreference say std::mutex is not movable? As per the documentation(https://en.cppreference.com/w/cpp/language/move_constructor), there are many preconditions that are not fulfilled that prevent the implicit move constructor. But I could not find the reason for this question. I would be

Why is there no need to mark the move constructor of a type with a deleted copy constructor as deleted?

♀尐吖头ヾ 提交于 2020-06-23 11:06:48
问题 Consider std::mutex . I understand why std::mutex should not be movable. But its copy constructor is clearly marked as deleted, but I have not seen such a declaration for its move constructor. So why does cppreference say std::mutex is not movable? As per the documentation(https://en.cppreference.com/w/cpp/language/move_constructor), there are many preconditions that are not fulfilled that prevent the implicit move constructor. But I could not find the reason for this question. I would be

Templated delegating copy constructor in constant expressions

荒凉一梦 提交于 2020-03-21 17:55:34
问题 This question is motivated by this one. Consider the following code: struct B {}; struct S { B b; // #1 S() = default; template <typename ...dummy> // #2 constexpr S(const S&) {} template <typename ...dummy> // #3 constexpr S(S &other) : S(const_cast<const S&>(other)) // #4 {} }; S s; constexpr S f() {return s;} int main() { constexpr auto x = f(); } GCC compiles this code successfully, but Clang rejects it (Example on Godbolt.org). The error message produced by Clang is <source>:21:20: error

Templated delegating copy constructor in constant expressions

本小妞迷上赌 提交于 2020-03-21 17:55:12
问题 This question is motivated by this one. Consider the following code: struct B {}; struct S { B b; // #1 S() = default; template <typename ...dummy> // #2 constexpr S(const S&) {} template <typename ...dummy> // #3 constexpr S(S &other) : S(const_cast<const S&>(other)) // #4 {} }; S s; constexpr S f() {return s;} int main() { constexpr auto x = f(); } GCC compiles this code successfully, but Clang rejects it (Example on Godbolt.org). The error message produced by Clang is <source>:21:20: error

Force compiler to choose copy constructor with const T& as a parameter

北城余情 提交于 2020-02-27 23:17:30
问题 I'm writing a class where I have a templated constructor and copy constructor. Every time I want to call copy constructor with non const object, templated constructor gets chosen. How can I force compiler to choose copy constructor? Here is the mcve: #include <iostream> struct foo { foo() { std::cout << "def constructor is invoked\n"; } foo(const foo& other) { std::cout << "copy constructor is invoked\n"; } template <typename T> foo(T&& value) { std::cout << "templated constructor is invoked

Copying structs with uninitialized members

限于喜欢 提交于 2020-02-26 08:48:30
问题 Is it valid to copy a struct some of whose members are not initialized? I suspect it is undefined behavior, but if so, it makes leaving any uninitialized members in a struct (even if those members are never used directly) quite dangerous. So I wonder if there is something in the standard that allows it. For instance, is this valid? struct Data { int a, b; }; int main() { Data data; data.a = 5; Data data2 = data; } 回答1: Yes, if the uninitialized member is not an unsigned narrow character type

Clone() vs Copy constructor- which is recommended in java [duplicate]

谁说我不能喝 提交于 2020-02-08 13:16:12
问题 This question already has answers here : clone() vs copy constructor vs factory method? (10 answers) Closed 4 years ago . clone method vs copy constructor in java. which one is correct solution. where to use each case? 回答1: Clone is broken, so dont use it. THE CLONE METHOD of the Object class is a somewhat magical method that does what no pure Java method could ever do: It produces an identical copy of its object. It has been present in the primordial Object superclass since the Beta-release

Clone() vs Copy constructor- which is recommended in java [duplicate]

拜拜、爱过 提交于 2020-02-08 13:15:48
问题 This question already has answers here : clone() vs copy constructor vs factory method? (10 answers) Closed 4 years ago . clone method vs copy constructor in java. which one is correct solution. where to use each case? 回答1: Clone is broken, so dont use it. THE CLONE METHOD of the Object class is a somewhat magical method that does what no pure Java method could ever do: It produces an identical copy of its object. It has been present in the primordial Object superclass since the Beta-release

How to utilize template copy&move constructor and assignment operator?

拜拜、爱过 提交于 2020-02-03 07:54:13
问题 Consider the following C++ code with my failed attempt to avoid preference of non-template copy&move constructors and assignment operators: template<typename T> class A { public: A() { /* implementation here */ } // Remove from the overloads the default copy&move constructors and assignment operators A(const A&) = delete; A& operator=(const A&) = delete; A(A&&) = delete; A& operator=(A&&) = delete; // I want these to be used e.g. by std::vector template<typename U> A(const A<U>& fellow) { /*

understanding virtual copy constructors

僤鯓⒐⒋嵵緔 提交于 2020-02-02 05:42:08
问题 I'm having trouble understanding what's really happening with the code in a book I'm using to learn C++. Here's the code: class Base { public: Base() {}; virtual ~Base() {}; virtual Base* Clone() {return new Base(*this);} }; class Derived { public: Derived() {}; virtual ~Derived() {}; virtual Base* Clone() {return new Derived(*this);} }; So in this Clone() function I understand that the function returns a pointer to a Base class object. What I don't understand is what's happening within that