Is there a bug in the Microsoft version of std::variant?

131 views Asked by At

With regards to the advice in this answer: How to guarantee copy elision with std::variant discovered after this question was closed.

TLDR: MSVC doesn't work with the advice given.

https://godbolt.org/z/r7Pr6eG1j

I have reduced my code to the following minimal example.

#include <variant>

struct Foo {
    int x;
    Foo(int _x):x(_x){}
    Foo(Foo&&) = delete;
    Foo(const Foo&) = delete;
};

using Storage = std::variant<std::monostate,Foo>;

template <typename F>
struct initializer {
    F f;
    template <typename T>
    operator T() const { 
        return f();
    }
};

template<class F>
initializer(F) -> initializer<F>;

Foo make_foo(){
    return Foo{10};
}

int main() {
    Storage s2;
    s2.emplace<1>(initializer{make_foo});
}

The code attempts to insert the non-copyable and non-movable type Foo into the variant by way of the wrapper type initializer. This pattern works for gcc and clang, but not for MSVC.

https://godbolt.org/z/r7Pr6eG1j

Is this a bug in the MSVC implementation of std::variant, or is the advice given in the original answer reliant on implementation defined behaviour?

Is there a workaround for MSVC?

image

0

There are 0 answers