<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 - Erroneous const this pointer in evaluation of constexpr constructors"
   href="https://bugs.llvm.org/show_bug.cgi?id=35256">35256</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Erroneous const this pointer in evaluation of constexpr constructors
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>5.0
          </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>drsam94@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When evaluating a constant expression which calls a constexpr constructor to
construct a local const or constexpr variable, the this pointer is erroneously
treated as const in the evaluation of the constructor, leading to erroneous
compilation errors.

These errors go away if the constructor is called for a local that is not
declared const or constexpr. Also initializing variables through default
initializers and other means does not cause this error, only code in the body
of the constructor that refers to this (or implicitly does so by accessing a
local variable)

A minimal example which fails on clang 5.0 is:

struct A {
    int x{};
    constexpr A() = default;
    constexpr A(int x) { 
        *this = A{};
    }
};

constexpr bool foo() {
    const A v{4};
    return v.x == 0;
}
static_assert(foo());

which fails with error:

14 : <source>:14:15: error: static_assert expression is not an integral
constant expression
static_assert(foo());
              ^~~~~
6 : <source>:6:17: note: modification of object of const-qualified type 'const
A' is not allowed in a constant expression
        *this = A{};

This example can be viewed in godbolt at:
<a href="https://godbolt.org/g/SMnwBq">https://godbolt.org/g/SMnwBq</a>

The failure seems to be in many versions of the compiler, it fails with both
--std=c++14 and --std=c++17 and on all versions of the compiler I have found
with C++14 support up to and including 5.0.

This seems clearly wrong by my reading of the standard, and is permitted in
other compilers such as gcc</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>