有没有办法从已知的替代方案中重置 std::variant?

Is there a way to reset a std::variant from a known alternative?

我正在更新当前使用 std::variant 到 C++17 的自定义等效代码库。

在代码的某些部分,变体正在从已知的替代项中重置,因此 class 提供了一种方法来断言 index() 处于当前值,但仍直接调用无条件地使用适当的析构函数。

这用于一些紧密的内部循环,并且具有(测量的)非平凡的性能影响。那是因为它允许编译器在所讨论的替代方案是可平凡破坏的类型时消除整个破坏。

从表面上看,在我看来,我无法通过 STL 中当前的 std::variant<> 实现来实现这一点,但我希望我错了。

有没有我没有看到的方法来完成这个,或者我不走运?

编辑: 应要求,这里有一个用法示例(以@T.C的示例为基础):

struct S {
    ~S();
};

using var = MyVariant<S, int, double>;

void change_int_to_double(var& v){
  v.reset_from<1>(0.0);
}

change_int_to_double 有效编译为:

@change_int_to_double(MyVariant<S, int, double>&)
  mov qword ptr [rdi], 0       // Sets the storage to double(0.0)
  mov dword ptr [rdi + 8], 2   // Sets the index to 2

编辑#2

多亏@T.C. 的各种见解,我找到了这个怪物。它 "works" 尽管它通过跳过一些析构函数确实违反了标准。但是,每个跳过的析构函数在编译时都会被检查为微不足道的,所以...:[=​​19=]

请参阅 godbolt:https://godbolt.org/g/2LK2fa

// Let's make sure our std::variant implementation does nothing funky internally.
static_assert(std::is_trivially_destructible<std::variant<char, int>>::value, 
          "change_from_I won't be valid");

template<size_t I, typename arg_t, typename... VAR_ARGS>
void change_from_I(std::variant<VAR_ARGS...>& v, arg_t&& new_val) {
    assert(I == v.index());

    // Optimize away the std::get<> runtime check if possible.
    #if defined(__GNUC__) 
      if(v.index() != I) __builtin_unreachable();
    #else
      if(v.index() != I) std::terminate();
    #endif


    // Smart compilers handle this fine without this check, but MSVC can 
    // use the help.
    using current_t = std::variant_alternative_t<I, std::variant<VAR_ARGS...>>;
    if(!std::is_trivially_destructible<current_t>::value) {
        std::get<I>(v).~current_t();
    }
    new (&v) var(std::forward<arg_t>(new_val));
}
#include <variant>
struct S {
    ~S();
};
using var = std::variant<S, int, double>;

void change_int_to_double(var& v){
    if(v.index() != 1) __builtin_unreachable();
    v = 0.0;
}

海湾合作委员会 compiles the function down to:

change_int_to_double(std::variant<S, int, double>&):
  mov QWORD PTR [rdi], 0x000000000
  mov BYTE PTR [rdi+8], 2
  ret

这是最优的。 Clang 的代码生成器 OTOH 有很多不足之处,尽管它 isn't too bad 如果您使用 std::terminate() (相当于断言)而不是 __builtin_unreachable():

change_int_to_double(std::__1::variant<S, int, double>&): # @change_int_to_double(std::__1::variant<S, int, double>&)
  cmp dword ptr [rdi + 8], 1
  jne .LBB0_2
  mov qword ptr [rdi], 0
  mov dword ptr [rdi + 8], 2
  ret
.LBB0_2:
  push rax
  call std::terminate()

MSVC...我们不谈 MSVC。