7

If we have code like this:

#include <variant>

int main(){
    using V = std::variant<int, double>;
    V a = 5;
    V b = 5.6;

    a.swap(b);
}

https://gcc.godbolt.org/z/oqGiHs

If you compile with clang, it emits code that handle exception during swap.

Why is that? Both variants are non empty and underlying types are exception safe.

Update:

Ironically this compiles without exception:

#include <variant>

template<class T>
void sw(T &a, T &b){
    auto c = a;
    a = b;
    b = c;
}

int main(){
    using V = std::variant<int, double>;

    V a = 5;
    V b = 5.6;

    sw(a, b);
}
3
  • GCC just emits code for implicit return 0; in my case. Commented Jun 16, 2020 at 8:34
  • You are correct. I will edit the question. Commented Jun 16, 2020 at 8:37
  • 1
    It's a bit weird since noexcept(a.swap(b)) is true with Clang, so it should be able to "know" that nothing in main can actually throw. Commented Jun 16, 2020 at 10:06

1 Answer 1

2

This turns out to be clang bug.

https://bugs.llvm.org/show_bug.cgi?id=46342

It appears to be fixed, but I can not find the version where is fixed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.