<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Constexpr constructor not initializing a union member is not diagnosed"
   href="https://bugs.llvm.org/show_bug.cgi?id=47345">47345</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Constexpr constructor not initializing a union member is not diagnosed
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++14
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>maltsevm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following C++14 code:

template <class> struct C {
    union {
      int i;
    };
    constexpr C() {}
};
constexpr C<int> c;

This code is ill-formed because the constructor C does not initialize the data
member i, but Clang fails to diagnose the error.

According to C++14 [dcl.constexpr]/4:
"The definition of a constexpr constructor shall satisfy the following
constraints:
- ...
In addition, either its function-body shall be = delete, or it shall satisfy
the following constraints:
- ...
- if the class is a union-like class, but is not a union, for each of its
anonymous union members having variant members, exactly one of them shall be
initialized;"

[dcl.constexpr]/6:
"If the instantiated template specialization of a constexpr function template
or member function of a class template would fail to satisfy the requirements
for a constexpr function or constexpr constructor, that specialization is still
a constexpr function or constexpr constructor, even though a call to such a
function cannot appear in a constant expression."

GCC produces the following diagnostic:
<source>:7:18: error: 'constexpr C< <template-parameter-1-1> >::C() [with
<template-parameter-1-1> = int]' called in a constant expression

    7 | constexpr C<int> c;

      |                  ^

<source>:5:15: note: 'constexpr C< <template-parameter-1-1> >::C() [with
<template-parameter-1-1> = int]' is not usable as a 'constexpr' function
because:

    5 |     constexpr C() {}

      |               ^

<source>:5:15: error: 'constexpr' constructor for union 'C<int>::<unnamed
union>' must initialize exactly one non-static data member</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>