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

    <tr>
        <th>Summary</th>
        <td>
            Pointer comparisons in constexpr functions having an unspecified value.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          warp-10
      </td>
    </tr>
</table>

<pre>
    The real situation is rather complex, but similar to the following:

<pre><code>
template< typename TargetPtr_t, typename SourcePtr_t, typename Count_t >
inline constexpr auto MyCopyTo(
 TargetPtr_t pTarget,
    SourcePtr_t pSource,
    Count_t count )
{
  // Error: Comparison has unspecified value.
  if( pTarget <= pSource )
  {
    while( count-- ){ *pTarget++ = *pSource++; }
 return pTarget;
  }
  else
  {
    pTarget += count;
 pSource += count;
    auto pTargetEnd = pTarget;
    while( count-- ){ *--pTarget = *--pSource; }
    return pTargetEnd;
 }
}

constexpr int Test()
{
    int u[]{1,2,3,4,5,6,7,8};
 int v[sizeof(u)/sizeof(u[0])]{};
    MyCopyTo( v, u,sizeof(u)/sizeof(u[0]));
    return 0;
}

int main( int argc, char **argv )
{
    constexpr auto v = Test();
    return v;
}
</code></pre>

The behavior of things like "MyCopyTo" are well-defined. In this case, the comparison is required for correct behavior where it is being used with overlapping ranges of data.

There is no reasonable workaround. This requires having to re-write calls to things like "MyCopyTo" by either using pointers into a single aggregate array (not always possible), or replacing the calls themselves with for loops.

I don't target gcc, though its behavior is the same here. I consider this to be a mis-compilation in either case.

MSC cl.exe does not report errors.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVc2O4jgQfhpzKYGCQwM5cGhCI_VhpJGm76NKUiTeMXbWP9Ds068qgQR62NVcErtcru-rX6P3qjZEG_GyFS-7CcbQWLc5o2un82RS2Oqy-WgIHKEGr0LEoKwB5cFhaMhBaY-tpk8hcyhiAK-OSq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