https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116731

            Bug ID: 116731
           Summary: Incorrect behavior of -Wrange-loop-construct in GCC 14
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sunil.dora1988 at gmail dot com
  Target Milestone: ---

It appears that the warning by -Wrange-loop-construct is triggered incorrectly
for certain loop constructs.

Consider the below example :

#include <array>
#include <cstdint>
#include <iostream>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

static std::string val{"vale"};

int main() {
    const std::vector arr{std::pair<std::string_view, int>{val, 1}};
    for (const auto [text, val] : arr) {
        std::cout << text << val;
    }
    return 0;
}

=============
g++ -pipe -W -Wall -Wextra -pedantic -g -O  -c -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:14:21: warning: loop variable ‘<structured bindings>’ creates a copy
from type ‘const std::pair<std::basic_string_view<char>, int>’
[-Wrange-loop-construct]
   14 |     for (const auto [text, val] : arr) {
      |                     ^~~~~~~~~~~
test.cpp:14:21: note: use reference type to prevent copying
   14 |     for (const auto [text, val] : arr) {
      |                     ^~~~~~~~~~~
      |                     &
g++ -g -o test test.o
================

It seems that GCC/G++ might be checking for trivial copyability rather than
trivial copy-constructibility, which could be contributing to the issue.

Reply via email to