<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 - Accidental equality of classes templated by pointer to local static constant of templated function"
   href="https://bugs.llvm.org/show_bug.cgi?id=48517">48517</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Accidental equality of classes templated by pointer to local static constant of templated function
          </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>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

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

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

        <tr>
          <th>Reporter</th>
          <td>matthieum.147192@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>Based on <a href="https://stackoverflow.com/q/65306562/147192">https://stackoverflow.com/q/65306562/147192</a>.

The behavior of Clang (and GCC) is inconsistent (between compile-time and
run-time), however it is unclear to me whether the inconsistency is conforming
with the C++17 standard or not.

Furthermore, in -O0 mode, Clang generates an unused symbol (`create()::I`)
which causes the linker to fail, see <a href="https://godbolt.org/z/M4T7f3">https://godbolt.org/z/M4T7f3</a>.


The following reduced program is expected to return 0 (invoking clang++ with
-std=c++17), it does not (<a href="https://godbolt.org/z/6r6vK3">https://godbolt.org/z/6r6vK3</a>):

template <typename, typename>
struct is_same { static constexpr bool value = false; };

template <typename T>
struct is_same<T, T> { static constexpr bool value = true; };

template <typename T, typename U>
static constexpr bool is_same_v = is_same<T, U>::value;

using uintptr_t = unsigned long long;

template <int const* I>
struct Parameterized { int const* member; };

template <typename T>
auto create() {
    static constexpr int const I = 2;

    return Parameterized<&I>{ &I };
}

int main() {
    auto one = create<short>();
    auto two = create<int>();

    if (is_same_v<decltype(one), decltype(two)>) {
        return reinterpret_cast<uintptr_t>(one.member) ==
reinterpret_cast<uintptr_t>(two.member) ? 1 : 2;
    }

    return 0;
}

Yet, on all versions of Clang where it compiles (from 5.0.0 onwards), and for
all optimization levels (from -O1 to -O3), it returns 2, indicating:

- That `one` and `two` have the same type -- which according to 17.4
[temp.type] should mean that they point to the same object.
- Yet they point to different objects -- there are two instances of
`create<T>()::I`, one for `T = short` and one for `T = int`.

The assembly listing clearly contains 2 different instances of
`create<T>()::I`.


Notes:

- If `I` is initialized with `= sizeof(T)`, instead, then with -O1 to -O3 the
program returns 0 as expected.
- Even with `I` initialized with `= sizeof(T)`, with -O0 Clang still generates
an unused symbol which causes the linker to fail: `auto create<short>()::I` and
`auto create<int>()::I`, whereas the declared symbols do not have the leading
`auto`.</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>