Search code examples
c++namespacesg++cygwinusing

STL error _without_ any STL in code


I am using gcc 3.4.4 on cygwin. I am getting this rather perplexing STL error message in my code below which does not use STL at all:

#include <iostream>


using namespace std;

const int N = 100;

bool s[N + 1];
bool p[N + 1];
bool t[N + 1];

void find(const bool a[], bool b[], bool c[]){
  return;
}


int main(){
  find(s, p, t);
  return 0;
}

When I compile with g++ stack.cc

I get the following error message:

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: In function `_RandomAccessIterator std::find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = bool*, _Tp = bool[101]]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:314:   instantiated from `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = bool*, _Tp = bool[101]]'
stack.cc:18:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:207: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:211: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:215: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:219: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:227: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:231: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:235: error: ISO C++ forbids comparison between pointer and integer

As you can see, the code does not use any STL at all, so this is rather strange. Also, the error disappears if I remove the line

using namespace std;

which hints at some namespace clash. It also disappears if I remove the const keyword from the definition o the function find.

On the other had the error also disappears (and this is rather surprising) if I make find a 2-argument function as follows:

#include <iostream>


using namespace std;

const int N = 100;

bool s[N + 1];
bool p[N + 1];
bool t[N + 1];

void find(const bool a[], bool b[]){
  return;
}


int main(){
  find(s, p);
  return 0;
}

I can't imagine what could be the reason why find can be a two argument function but not a three argument one.

So here is a brief summary of the three ways to remove the error:

  1. Remove the using namespace std; line.

  2. Remove the const keyword from the definition of find.

  3. Remove the third argument of the function find.

I cannot think of any logical reason why such an error should happen in the first place, and why it should get removed i I use any of the above seemingly completely unrelated steps. Is this a documented g++ bug? I tried searching for it, but honestly I was at a loss what to search for, and the few keywords I tried ("STL error without STL use") didn't turn up anything.


Solution

  • You simply have a collision, because you've unintentionally pulled std::find (which takes 3 arguments) into the global namespace when you did using namespace std;. For whatever reason, your <iostream> is #include-ing <algorithm>, or one of the parts of its internal implementation (specifically, bits/stl_algo.h).

    I can't explain why removing const makes it go away; perhaps it affects the order in which the compiler resolves overloads.