Search code examples
c++c++20standards

Did C++20 change to allow conversion from array-of-known-bound `type(&)[N]` to array-of-unknown-bound `type(&)[]`?


Context:

In C++, it's possible to create a reference to an "array of unknown bound".

Example: const char (&)[]

Before C++20, clang (14.x, 18.x) did not allow you to coerce a reference to an array of known bounds to a reference to an array of unknown bounds, and would fail with an error:

reference to incomplete type 'const char[]' could not bind to an lvalue of type 'const char[5]'

For C++20, clang now allows this.

gcc, on the other hand, has always allowed it, all the way back to C++98.

Questions:

  • Is this just a disagreement over standards?
  • Did one of them have it wrong?
  • Did C++20 change or clarify the standard that makes this more clearly valid?

Example

https://godbolt.org/z/66sh8a5z1

int main() {
    using StringLitRef = const char (&)[];
    StringLitRef array = "derp"; // works on g++ for any standard and clang with c++20
    return 0;
}

Solution

  • This was P0388R4 (Permit conversions to arrays of unknown bound), which was adopted in the Cologne meeting (July 2019) for C++20.