expected-dark
expected-dark copied to clipboard
std::swap noexcept workaround is inaccurate
The commit b74e3a9 fixes clang 4.0's complaint, but still exhibits a bug. The noexcept specification will check the noexceptness of std::swap(E&, E&), but inside the function, ADL is used, and so swap(E&,E&) may resolve to another swap function besides std::swap. This means that the noexceptness of the function does not match the noexcept declaration. This could cause serious failures.
Thanks @jeremysalwen,
In this respect, expected-lite originally was like optional by Andrzej Krzemieński.
It appears that the problem (only) occurs with value type void
with with clang 4,5,6 (see wandbox example).
Why would someone use expected<void, std::error_code>
?
Argh, missed the fact there's a void specialisation... Updated wandbox example.
What makes that optional
does not have the problem?
I am not sure what is causing the error to not appear in certain circumstances, but it is clear to me that there is a bug in the optional example you linked to as well. noexcept(swap(...))
will not consider std::swap, and noexcept(std::swap(...))
will not consider swaps in other namespaces. Inside the function, ADL is used which could resolve to either. The noexcept definition needs to match that usage. Unfortunately I don't think you can't put a using std::swap;
inside the noexcept
expression, so it seems like you should instead use something like std::is_nothrow_swappable<T>. However, that's c++17 only, and would need to be backported.