0

This code compiles with g++, no collision is detected, although s is the parameter captured by lambda and the lambda parameter. My compiler is g++.

gcc Version is

gcc (Debian 4.9.2-10+deb8u2) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.

and I am using these compilation flags :

-W 
-Wall
-ansi
-pedantic
-s 
-march=native
-flto 
-fwhole-program 
-Wfatal-errors 
-Wextra 
-std=c++1y 
-frename-registers 
-fipa-pta 
-Ofast 
-pedantic-errors 
-fira-loop-pressure
-fomit-frame-pointer
-fforce-addr 
-falign-functions 
-fno-cprop-registers 
-fstrength-reduce

Code is

#include <vector>
#include <algorithm>
#include <iostream>

int main (int argc, char* argv []) {
  int s (0);
  std::vector<int> vec ({3, 5, 13, 1});
  std::for_each (vec.begin (), vec.end (), [&s] (const int& s) {
    s = s>s?s:s;
  });
  std::cout << "max ox v is " << s << std::endl;
}

Is there a compilation option to detect this kind of error ?

Saint-Martin
  • 306
  • 3
  • 16

1 Answers1

0

Upgrade your compilers.

Based on https://godbolt.org/z/h3Y7dW1dP that warning is missing for gcc 8.5 (with -std=c++17) and clang 7.1.0.

You should upgrade to gcc 9.1 and clang 8.0.0 or later. (Preferably a lot later.)

Hans Olsson
  • 11,123
  • 15
  • 38