为什么clang会对这种琐碎的std::variant代码进行异常处理?

问题描述 投票:3回答:1

如果我们有这样的代码

#include <variant>

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

    a.swap(b);
}

https:/gcc.godbolt.orgzoqGiHs

如果你用clang编译,它在编译过程中会发出处理异常的代码。swap.

为什么会这样?两个变体都是非空的,而且底层类型是异常安全的。

更新。

具有讽刺意味的是,这在编译时没有异常。

#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);
}
c++ clang c++17 variant
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.