c++templatesc++17move-semanticsrule-of-five

Why is a class with an explicitly declared move constructor failing `std::is_move_constructible_v`?


A library I'm using requires that classes used with its templates are move constructible and assignable, for various internal reasons. I've written a class, and given it an explicit move constructor.

#pragma once
#include <entt.hpp>

namespace ECS {
    //! Abstract base class for all components.
    class Component {
        friend class Transform;
    private:
        entt::registry* p_registry; //!< The registry to which this component belongs.
        entt::entity m_entity = entt::null; //!< The entity to which this component belongs.
        bool m_knownEntity = false;

        virtual void checkEntity() {
            m_knownEntity = true;
            m_entity = entt::to_entity(*p_registry, *this);
        }
    public:
        Component(entt::registry& registry) :
            p_registry(&registry) {}

        Component(Component&& old) noexcept :
            p_registry(nullptr) {
            *this = std::move(old);
        }
        virtual Component& operator=(Component&& old) {
            if (this != &old) {
                p_registry = old.p_registry;
                m_entity = old.m_entity;
                m_knownEntity = false;
            }
            return *this;
        }

        inline entt::entity getEntity() { if (m_knownEntity) { checkEntity(); } return m_entity; }
        entt::registry& getRegistry() { return *p_registry; }

        virtual void initialise() = 0; //!< Called once per component. During object creation, is called after all the object's components have been created.
        virtual void terminate() = 0; //!< Called once per component. During object destruction, is called before any of the object's components have been deleted.
    };
}

However, the first of these static assertions fails:

static_assert(std::is_move_constructible_v<ECS::Component>, "ECS::Component isn't move constructible."); // Fails
static_assert(std::is_move_assignable_v<ECS::Component>, "ECS::Component isn't move assignable."); // Passes

What have I missed? This is really puzzling, and I can't find anything online about why a class with an explicitly declared move constructor might not be move constructible. Is it because it's an abstract class? Any help would be greatly appreciated.


Solution

  • is_move_constructible is based on the behavior of is_constructibe; it's just is_constructible<T, T&&>. And is_constructible<T, ...> requires that you can do this:

    T t(...);
    

    Of course, for an abstract class, this is something you cannot do. Therefore, no abstract class can be "move constructible" as far as is_move_constructible is concerned.