https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87431

ensadc at mailnesia dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ensadc at mailnesia dot com

--- Comment #7 from ensadc at mailnesia dot com ---
This "optimization" might be unsafe in the presence of throwing conversion
operator:

// https://wandbox.org/permlink/ADEIfSv120QMrKeS
#include <variant>
#include <cassert>

struct weird {
    operator int() { throw 0; }
};

int main() {
    std::variant<char, int> v;
    try {
        v.emplace<1>(weird{});
    } catch (...) {
        assert((v.index() == std::variant_npos) ==
(v.valueless_by_exception()));
    }
}

Maybe we can fix the optimization by making `emplace` restore the original
value (or not destroy in the first place) when the optimization is present and
an exception is thrown.

Reply via email to