title: std::variant of std::string inside any class in constexpr context fails to compile
body: <p>The following code</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;optional&gt;
  #include &lt;string&gt;
  #include &lt;variant&gt;
  
  constexpr bool USE_VARIANT = 1;
  
  using T = std::conditional_t&lt;USE_VARIANT, std::variant&lt;std::string&gt;, std::string&gt;;
  
  struct S
  {
      T t;
  };
  
  constexpr int func()
  {
      S s{&quot;str&quot;};
      return 0;
  }
  
  constexpr int x = func();
  
  int main()
  {
  }
  </code></pre>
  <p>fails to compile with GCC trunk (<a href="https://godbolt.org/z/e16693Wrh" rel="noreferrer">compiler explorer link</a>) with the message</p>
  <pre><code>In file included from &lt;source&gt;:4:
  &lt;source&gt;:21:23:   in 'constexpr' expansion of 'func()'
  &lt;source&gt;:19:1:   in 'constexpr' expansion of '(&amp; s)-&gt;S::~S()'
  &lt;source&gt;:10:8:   in 'constexpr' expansion of '((S*)this)-&gt;S::t.std::variant&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::~variant()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:1408:28:   in 'constexpr' expansion of '((std::variant&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;*)this)-&gt;std::variant&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Variant_base&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::~_Variant_base()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:736:12:   in 'constexpr' expansion of '((std::__detail::__variant::_Variant_base&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;*)this)-&gt;std::__detail::__variant::_Variant_base&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Move_assign_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::~_Move_assign_base()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:682:12:   in 'constexpr' expansion of '((std::__detail::__variant::_Move_assign_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;*)this)-&gt;std::__detail::__variant::_Move_assign_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Copy_assign_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::~_Copy_assign_base()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:630:12:   in 'constexpr' expansion of '((std::__detail::__variant::_Copy_assign_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;*)this)-&gt;std::__detail::__variant::_Copy_assign_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Move_ctor_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::~_Move_ctor_base()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:592:12:   in 'constexpr' expansion of '((std::__detail::__variant::_Move_ctor_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;*)this)-&gt;std::__detail::__variant::_Move_ctor_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Copy_ctor_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::~_Copy_ctor_base()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:555:12:   in 'constexpr' expansion of '((std::__detail::__variant::_Copy_ctor_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;*)this)-&gt;std::__detail::__variant::_Copy_ctor_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Variant_storage&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::~_Variant_storage()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:481:17:   in 'constexpr' expansion of '((std::__detail::__variant::_Variant_storage&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;*)this)-&gt;std::__detail::__variant::_Variant_storage&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::_M_reset()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:471:23:   in 'constexpr' expansion of 'std::__do_visit&lt;void, __detail::__variant::_Variant_storage&lt;false, __cxx11::basic_string&lt;char, char_traits&lt;char&gt;, allocator&lt;char&gt; &gt; &gt;::_M_reset()::&lt;lambda(auto:11&amp;&amp;)&gt;, variant&lt;__cxx11::basic_string&lt;char, char_traits&lt;char&gt;, allocator&lt;char&gt; &gt; &gt;&amp;&gt;(&lt;lambda closure object&gt;std::__detail::__variant::_Variant_storage&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::_M_reset()::&lt;lambda(auto:11&amp;&amp;)&gt;(), (* &amp; std::__variant_cast&lt;__cxx11::basic_string&lt;char, char_traits&lt;char&gt;, allocator&lt;char&gt; &gt;, __detail::__variant::_Variant_storage&lt;false, __cxx11::basic_string&lt;char, char_traits&lt;char&gt;, allocator&lt;char&gt; &gt; &gt;&amp;&gt;((*(std::__detail::__variant::_Variant_storage&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;*)this))))'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:1782:26:   in 'constexpr' expansion of '(&amp; __v0)-&gt;std::variant&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::index()'
  /opt/compiler-explorer/gcc-trunk-20211231/include/c++/12.0.0/variant:555:12: error: accessing value of 's.S::t.std::variant&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Variant_base&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Move_assign_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Copy_assign_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Move_ctor_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;.std::__detail::__variant::_Copy_ctor_base&lt;false, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::&lt;anonymous&gt;' through a 'const std::variant&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;' glvalue in a constant expression
    555 |     struct _Copy_ctor_base : _Variant_storage_alias&lt;_Types...&gt;
        |            ^~~~~~~~~~~~~~~
  </code></pre>
  <p>C++ errors are often difficult to understand, but this one just left me clueless.</p>
  <p>If I use <code>USE_VARIANT = 0</code>, it compiles.</p>
  <p>If I change it to</p>
  <pre class="lang-cpp prettyprint-override"><code>constexpr int func()
  {
      T t{&quot;str&quot;};
      return 0;
  }
  </code></pre>
  <p>it compiles with either value.</p>
  <p>With</p>
  <pre class="lang-cpp prettyprint-override"><code>constexpr int func()
  {
      std::optional&lt;T&gt; t{&quot;str&quot;};
      return 0;
  }
  </code></pre>
  <p>it doesn't compile either.</p>
  <p>So it seems that when a <code>std::variant&lt;..., std::string, ...&gt;</code> is instantiated in a class, it fails to compile. The same happens with a <code>std::vector&lt;char&gt;</code> instead of a <code>std::string</code>, but other classes (that don't allocate memory dynamically) seem to work fine.</p>
  <p>Am I missing something or have I found a compiler/stdlib bug?</p>