diff options
author | Marek Polacek <polacek@redhat.com> | 2020-11-09 21:15:33 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2020-11-21 16:20:16 -0500 |
commit | c51e31a06f2c740c55852a683aa7ffdc20417362 (patch) | |
tree | 8e9427d27fc773b8f550807538315fc9bc9e6160 /gcc/doc | |
parent | 6f20c42cc162ac3725584547ab4933bae4c78665 (diff) | |
download | gcc-c51e31a06f2c740c55852a683aa7ffdc20417362.zip gcc-c51e31a06f2c740c55852a683aa7ffdc20417362.tar.gz gcc-c51e31a06f2c740c55852a683aa7ffdc20417362.tar.bz2 |
c++: Extend -Wrange-loop-construct for binding-to-temp [PR94695]
This patch finishes the second half of -Wrange-loop-construct I promised
to implement: it warns when a loop variable in a range-based for-loop is
initialized with a value of a different type resulting in a copy. For
instance:
int arr[10];
for (const double &x : arr) { ... }
where in every iteration we have to create and destroy a temporary value
of type double, to which we bind the reference. This could negatively
impact performance.
As per Clang, this doesn't warn when the range returns a copy, hence the
glvalue_p check.
gcc/ChangeLog:
PR c++/94695
* doc/invoke.texi: Update the -Wrange-loop-construct description.
gcc/cp/ChangeLog:
PR c++/94695
* parser.c (warn_for_range_copy): Warn when the loop variable is
initialized with a value of a different type resulting in a copy.
gcc/testsuite/ChangeLog:
PR c++/94695
* g++.dg/warn/Wrange-loop-construct2.C: New test.
Diffstat (limited to 'gcc/doc')
-rw-r--r-- | gcc/doc/invoke.texi | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 02abac3..26372a2 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -3681,7 +3681,23 @@ void fn () @{ @end smallexample It does not warn when the type being copied is a trivially-copyable type whose -size is less than 64 bytes. This warning is enabled by @option{-Wall}. +size is less than 64 bytes. + +This warning also warns when a loop variable in a range-based for-loop is +initialized with a value of a different type resulting in a copy. For example: + +@smallexample +void fn() @{ + int arr[10]; + for (const double &x : arr) @{ @dots{} @} +@} +@end smallexample + +In the example above, in every iteration of the loop a temporary value of +type @code{double} is created and destroyed, to which the reference +@code{const double &} is bound. + +This warning is enabled by @option{-Wall}. @item -Wredundant-tags @r{(C++ and Objective-C++ only)} @opindex Wredundant-tags |