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
int main() {
using StringLitRef = const char (&)[];
StringLitRef array = "derp"; // works on g++ for any standard and clang with c++20
return 0;
}
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./z/66sh8a5z1
int main() {
using StringLitRef = const char (&)[];
StringLitRef array = "derp"; // works on g++ for any standard and clang with c++20
return 0;
}
Share
edited Mar 4 at 16:28
Catskul
asked Mar 3 at 22:57
CatskulCatskul
19.5k16 gold badges88 silver badges120 bronze badges
1
- 4 +1 for telling us about the references to arrays of unknown bounds. I was only aware of references to fixed-size arrays, and all of my code with array references is templated on the array size. Maybe I can get rid of that template parameter? – einpoklum Commented Mar 3 at 23:41
1 Answer
Reset to default 23This was P0388R4 (Permit conversions to arrays of unknown bound), which was adopted in the Cologne meeting (July 2019) for C++20.