How can I check if a move constructor is being generated implicitly?

前端 未结 3 2072
一向
一向 2021-02-04 04:20

I have several classes for which I wish to check whether a default move constructor is being generated. Is there a way to check this (be it a compile-time assertion, or parsing

3条回答
  •  春和景丽
    2021-02-04 04:58

    Declare the special member functions you want to exist in MyStruct, but don't default the ones you want to check. Suppose you care about the move functions and also want to make sure that the move constructor is noexcept:

    struct MyStruct {
        MyStruct() = default;
        MyStruct(const MyStruct&) = default;
        MyStruct(MyStruct&&) noexcept; // no = default; here
        MyStruct& operator=(const MyStruct&) = default;
        MyStruct& operator=(MyStruct&&); // or here
    };
    

    Then explicitly default them, outside the class definition:

    inline MyStruct::MyStruct(MyStruct&&) noexcept = default;
    inline MyStruct& MyStruct::operator=(MyStruct&&) = default;
    

    This triggers a compile-time error if the defaulted function would be implicitly defined as deleted.

提交回复
热议问题