Best style for deleting all constructors (or other function)?

主宰稳场 提交于 2019-12-04 03:08:52

There is a difference, for example:

#include <type_traits>

struct Unconstructible
{
    Unconstructible() = delete;
};

static_assert( !std::is_default_constructible<Unconstructible>::value, "not default constructible" );
static_assert( std::is_copy_constructible<Unconstructible>::value, "copyable" );

Although you can never construct this object, so in practice you can never create a copy of one either, according the language and the type traits in the library, it is technically CopyConstructible, because there's an implicitly-declared copy constructor.

Similarly, with the Impossible(...) or Impossible(Ts&&...) forms there is still an implicitly-declared copy constructor.

On the other hand, if you do it this way:

#include <type_traits>

struct Unconstructible
{
    Unconstructible(const Unconstructible&) = delete;
};

static_assert( !std::is_default_constructible<Unconstructible>::value, "not default constructible" );
static_assert( !std::is_copy_constructible<Unconstructible>::value, "not copyable" );

The existence of a user-declared constructor suppresses the implicit declaration of the default constructor, so the class is neither DefaultConstructible nor CopyConstructible.


N.B. your last example should probably be Impossible(Ts&&...) to match any types, including non-copyable and non-movable ones.

The first one is more than enough - no constructors will be generated by the compiler and, most imporatantly, it's idiomatic.

You cannot create instance of this class:

struct Impossible
{
    Impossible() = delete;
};

Note the question of copy-constructor (or move-constructor) doesn't even arise, so deleting them is unnecessary. An object cannot exist because it cannot be constructed, so the copying or moving is out of question.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!