<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57669>57669</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Cannot change alternative of std::variant using emplace in a constexpr function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          cvuchener
      </td>
    </tr>
</table>

<pre>
    Trying to compile 

```c++
#include <variant>

constexpr auto f(bool b)
{
    std::variant<int, float> v;
    if (b)
        v.emplace<int>();
    else
        v.emplace<float>();
    return v;
}

constexpr auto v1 = f(true);
constexpr auto v2 = f(false);

int main()
{
    return 0;
}
```

fails with

```<source>:14:16: error: constexpr variable 'v2' must be initialized by a constant expression
constexpr auto v2 = f(false);
               ^    ~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/13.0.0/../../../../include/c++/13.0.0/variant:309:52: note: member call on member '_M_rest' of union with active member '_M_first' is not allowed in a constant expression
        return std::forward<_Union>(__u)._M_rest._M_first._M_get();
                                                          ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/13.0.0/../../../../include/c++/13.0.0/variant:539:39: note: in call to '__get_n(v._Variant_storage::_M_u)'
      auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u));
                                      ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/13.0.0/../../../../include/c++/13.0.0/variant:1519:8: note: in call to '__emplace(v)'
              __variant::__emplace<_Np>(*this, std::forward<_Args>(__args)...);
              ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/13.0.0/../../../../include/c++/13.0.0/variant:1493:17: note: in call to '&v->emplace()'
          return this->emplace<__index>(std::forward<_Args>(__args)...);
                       ^
<source>:9:11: note: in call to '&v->emplace()'
        v.emplace<float>();
          ^
<source>:14:21: note: in call to 'f(false)'
constexpr auto v2 = f(false);
```

This happens using libstdc++ (libc++ does not have constexpr std::variant). I am not sure if this is really a clang issue or an incompatibility from libstdc++. But it does work using gcc. Tell me if I should report this to libstdc++ instead.

I found the bug using Fedora 36 clangd, but I confirmed it using "x86-64 clang (trunk)" from Godbolt's compiler explorer.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVsuSqzYQ_Rq8UZkCYcBeeOE7j9QsktVNtpSAxlauLLkkwczk69PNw8Z3PFOZJIuhKIFxqx_ndKu7NPXr9rt9lXrPvGGVOZ6kAhZE90G0G9csGu4q4N_oHr7yROpKtTUKJ3edsFJoHyQP852V0c7Dy8ky0aLyJuDr0hjFyoBvRrl8VMfwcr4Okh3eZ213Eld-xxplBClnXZDMNsiGkcqzNjZeXQjHkxIVjBrQK74mqflmUA7e3zZZvLHRgm-tnrkS5PcfRN3FCNB9H7y3Lcy1_SzJz5KNIO9mosOKwbCjkHr06i2Eo2_RDd8mFuf6GiGVY8_SH24Sjjg401oC5AFpiVe0ZLgwsNZYermE0JNWUu7wvOO4sGPrPCuBSS29FEr-BTUrX5kYNiHBjDaCc9LoTwPCrq8gfegf-cPte8zZR3PChHoc09wu0ZYyFix-21fV0mlxcgdDIkqWw0dcX9ZZka2WSur2ZbnXLX6KkzAKI3wJw7fLWBhkaCyZ2YZzcu-SCMPZpZxw1MYDPY9wLMGySijFjJ5-IprFrwVC5QlX07BWI2Y9cUxUXnZwLdlIO4hKR5oZajPPiL7UH6E_YTkm0bkcG2OfhcUfd8XvZHcoi6JAHDbh6Fc4WaWXPfgbdfPvLiT2S5KXJkRev5zJQ3h74jB1iQfCoaBa7cLij2Fj4byxYg8DsAgVYYiyc5Qo9QO-Y0Uh6tr2FXCmYviGpJmmp-DiT__nYBB5-u00kdSFZyufpeOrQh-nMcG-_gj66SRH8N8iPF1v8Luc_2cEkQl_kI660K2C2Nm9m6AW9I4lQbG8h_WXxXS1SeiRvw9qwL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