lundi 30 janvier 2017

Objects of non-movable classes not throwable?

When compiled with Clang 3.9.1 or GCC 6.3.0 throwing movable but not copyable objects seems to work fine:

struct MovableNonCopyable {
    MovableNonCopyable();
    ~MovableNonCopyable();
    MovableNonCopyable(MovableNonCopyable &&);
    MovableNonCopyable(MovableNonCopyable const &) = delete;
    MovableNonCopyable & operator=(MovableNonCopyable &&);
    MovableNonCopyable & operator=(MovableNonCopyable const &) = delete;
};

void f() { throw MovableNonCopyable(); }

But throwing copyable but not movable objects like this:

struct CopyableNonMovable {
    CopyableNonMovable();
    ~CopyableNonMovable();
    CopyableNonMovable(CopyableNonMovable &&) = delete;
    CopyableNonMovable(CopyableNonMovable const &);
    CopyableNonMovable & operator=(CopyableNonMovable &&) = delete;
    CopyableNonMovable & operator=(CopyableNonMovable const &);
};

void g() { throw CopyableNonMovable(); }

instead causes compilation error like:

test.cpp: In function 'void g()':
test.cpp:21:41: error: use of deleted function 'CopyableNonMovable::CopyableNonMovable(CopyableNonMovable&&)'
    void g() { throw CopyableNonMovable(); }
                                        ^
test.cpp:15:9: note: declared here
        CopyableNonMovable(CopyableNonMovable &&) = delete;
        ^~~~~~~~~~~~~~~~~~

Why is this? According to [except.throw#5] this should be the other way around, i.e. the copy constructor should be accessible.

Aucun commentaire:

Enregistrer un commentaire