最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览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

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
Add a comment  | 

1 Answer 1

Reset to default 23

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论