Category : move-semantics

In the C++ standard vector.capacity section, it defines two overloads for resize(). (see also https://en.cppreference.com/w/cpp/container/vector/resize) This overload requires that the type T is MoveInsertable and DefaultInsertable: constexpr void resize(size_type sz); The other overload requires that the type T is CopyInsertable: constexpr void resize(size_type sz, const T& c); Clause 16 of the mentioned section in the ..

Read more

In the C++ standard vector.capacity section, it defines two overloads for resize(). (see also https://en.cppreference.com/w/cpp/container/vector/resize) This overload requires that the type T is MoveInsertable and DefaultInsertable: constexpr void resize(size_type sz); The other overload requires that the type T is CopyInsertable: constexpr void resize(size_type sz, const T& c); My question is that why doesn’t the second ..

Read more

According to cppreference, deriving from a non-movable class should make a derived class non-movable, too. Then why does std::is_move_constructible_v return true for the derived class? class NonMovable{ public: NonMovable(const NonMovable&) = default; NonMovable(NonMovable&&) = delete; NonMovable& operator = (const NonMovable&) = default; NonMovable& operator = (NonMovable&&) = delete; NonMovable() = default; }; class Derived : ..

Read more

According to the standard, you can only do certain things with moved-from objects. However, what is technically considered a moved-from objects? let’s say I do this: int x = 42; std::move(x); int y = x; // is this ok? std::move is just a cast to a rvalue-reference. If this reference is not consumed, does that ..

Read more

Mostly all things explained by fredoverflow(user 237K Rep.) in his Two answers https://stackoverflow.com/a/3109981/11862989 https://stackoverflow.com/a/11540204/11862989 But while implementing Move constructor and overloaded Move Assignment operator(OMAO)( I am using these short form throughout the question ) I am facing some problem that I will put here. Also there is another answer by user Greg Hewgill (user with ..

Read more

Assuming it’s possible, how to move an object’s data when it is a member object.. Consider the following code. class NameValue { private: CustomString mName; CustomeString mValue; public: NameVlaue(const NameValue &rhs) { /* omitted for this post */ } NameValue(const CustomString &name) : mName(name) {} NameValue(CustomString &&name) { mName.move(name); } NameValue &operator =(CustomString &&rhv) { ..

Read more

I have the generic class which can store a value and a type of the value encoded as a string. #include <iostream> #include <string> #include <memory> #include <cassert> struct IValueHolder { virtual ~IValueHolder() = default; virtual const std::string& getType() = 0; virtual void setType(const std::string& t_type) = 0; }; template<class T> class ValueHolder : public ..

Read more