Issue 63829
Summary clang wrongly believes that std::variant is not constexpr constructible if any Ti is not constexpr constructible
Labels new issue
Assignees
Reporter wanghan02
    Example could be found below or on [Compiler Explorer](https://godbolt.org/z/bx86vEa9e). clang trunk (and previous versions) does not compile while gcc and VC++ does.

```
#include <variant>

struct S1 {
    constexpr S1(int) {};
};

struct S2 {
    S2() {}
};

constexpr std::variant<S1, S2> var(S1(2)); // clang error: constexpr variable cannot have non-literal type 'const std::variant<S1, S2>'
constexpr std::variant<S1, S2> var2(std::in_place_type<S1>, 2); // clang error: constexpr variable cannot have non-literal type 'const std::variant<S1, S2>'
```

According to [variant.ctor#19](https://timsong-cpp.github.io/cppwp/n4868/variant.ctor#19) and [variant.ctor#34](https://timsong-cpp.github.io/cppwp/n4868/variant.ctor#34):

> Remarks: The _expression_ inside noexcept is equivalent to is_­nothrow_­constructible_­v<Tj, T>[.](https://timsong-cpp.github.io/cppwp/n4868/variant.ctor#19.sentence-1) If Tj's selected constructor is a constexpr constructor, this constructor is a constexpr constructor[.](https://timsong-cpp.github.io/cppwp/n4868/variant.ctor#19.sentence-2)

> Remarks: If TI's selected constructor is a constexpr constructor, this constructor is a constexpr constructor[.](https://timsong-cpp.github.io/cppwp/n4868/variant.ctor#34.sentence-1)

If the selected constructor is a constexpr constructor, std::variant's converting constructor is also a constexpr constructor. IMO clang is wrong here. I haven't checked other constructors, probably have the same behavior as these 2 above.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to