Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22369

Different behavior in candidate selection between GCC 11 and GCC 12

$
0
0

I'm encountering a different behavior when compiling the following code with GCC 11 and GCC 12 on Ubuntu 22.04:

#include <iostream>#include <algorithm>namespace Foo {  class MyClass {  private:    int a;  public:    int getA() const { return a; }  };}bool operator==(const Foo::MyClass &lhs, const Foo::MyClass &rhs) {  return lhs.getA() == rhs.getA();}int main() {  Foo::MyClass myClass1;  Foo::MyClass myClass2;  Foo::MyClass *p1 = &myClass1;  Foo::MyClass *p2 = &myClass2;  bool equal = std::equal(p1, p1 + 1, p2);  std::cout << "equal: " << equal << std::endl;  return 0;}

The detail compilation command (from cmake's verbose output) is:

/usr/bin/c++   -std=gnu++11 -MD -MT CMakeFiles/cmake-lab.dir/src/main.cpp.o -MF CMakeFiles/cmake-lab.dir/src/main.cpp.o.d -o CMakeFiles/cmake-lab.dir/src/main.cpp.o -c /home/wbhu/dev/CodeShare/cpp-proj/cmake-lab/src/main.cpp

When compiling with GCC 11, I get the following error:

/home/wbhu/dev/CodeShare/cpp-proj/cmake-lab/src/main.cpp:30:26:   required from here/usr/include/c++/11/bits/stl_algobase.h:1162:29: error: no match for ‘operator==’ (operand types are ‘Foo::MyClass’ and ‘Foo::MyClass’) 1162 |             if (!(*__first1 == *__first2))      |                  ~~~~~~~~~~~^~~~~~~~~~~~~

It seems that GCC 11 cannot find the operatior== defined outside of namespace Foo. However, the code compiles without any issues in GCC 12.

Interestingly, if I move MyClass out of the Foo namespace or move operator== inside the Foo namespace, the code compiles correctly in both GCC 11 and GCC 12

My questions are:

  • Which compiler's behavior is correct here?
  • What is causing this difference in behavior between GCC 11 and GCC 12?

Any insights or clarifications would be greatly appreciated!


Viewing all articles
Browse latest Browse all 22369

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>