2

A minimally interesting typo-bug

 2 years ago
source link: https://quuxplusone.github.io/blog/2022/01/29/missing-tilde/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A minimally interesting typo-bug

I just ran into some code like this in a test suite:

struct MoveOnlyWidget {
    MoveOnlyWidget(int);
    MoveOnlyWidget(const MoveOnlyWidget&) = delete;
    MoveOnlyWidget(MoveOnlyWidget&&) = default;
    MoveOnlyWidget& operator=(const MoveOnlyWidget&) = delete;
    MoveOnlyWidget& operator=(MoveOnlyWidget&&) = default;
    MoveOnlyWidget() = default;
};

I suspect that the original author intended that last line to have a ~ in front of MoveOnlyWidget — this type ends up being default-constructible when maybe it wasn’t intended to be.

It occurs to me that it would be neat if something like PVS-Studio gave a warning for a defaulted default constructor that wasn’t at the top of the class — or maybe any constructor that wasn’t grouped with the other constructors of the class. (Maybe PVS-Studio already does! I don’t know.)


Anyway, if it really mattered that MoveOnlyWidget shouldn’t be default-constructible, the original author could have enforced that by adding any one of these lines after the class’s closing brace:

static_assert(!std::is_constructible_v<MoveOnlyWidget>);
static_assert(!std::is_default_constructible_v<MoveOnlyWidget>);
static_assert(!std::default_initializable<MoveOnlyWidget>);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK