This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

-pedantic, and templates, and iterators --> Oh my!


Greetings:

	Sorry about the subject line.

	This one had me scratching my head for a long time.  I was
wondering why all my code compiled and worked just fine, but some of
my co-workers just couldn't get it to compile.  (Isn't it always that
way?)  Basically, the problem is that the combination of -pedantic,
templates, and the STL, results in parse errors.

	Using G++ 2.95.2 on a SMP Linux 2.2.12 box, the following code
compiles just fine with 'g++ -Wall', but fails with
'g++ -Wall -pedantic' and produces the following error messages:

---
g++_bug.cc: In method `DataType SimpleClass<DataType>::addThem() const':
g++_bug.cc:13: parse error before `('
g++_bug.cc: In method `double SimpleClass<double>::addThem() const':
g++_bug.cc:40:   instantiated from here
g++_bug.cc:18: `end' undeclared (first use this function)
g++_bug.cc:18: (Each undeclared identifier is reported only once
g++_bug.cc:18: for each function it appears in.)
g++_bug.cc:18: `itr' undeclared (first use this function)
---

Here's the "minimal" test program:

---
#include <iostream>
#include <list>

template <class DataType>
class SimpleClass {
public:
  SimpleClass(list<DataType> const & them)
    : _them(them.begin(), them.end())
  {}

  DataType addThem() const {
    list<DataType>::const_iterator
      itr(_them.begin()),
      end(_them.end());

    DataType total(static_cast<DataType>(0));

    while (itr != end) {
      total += *itr;
      ++itr;
    }

    return total;
  }

private:
  list<DataType> _them;
};

int
main()
{
  list<double> them;

  for (int i = 0; i < 10; i++)
    them.push_back(static_cast<double>(i));

  SimpleClass<double> simple_class(them);

  cout << simple_class.addThem() << endl;

  return 0;
}
---

Note that if you rewrite this code so that it doesn't use iterators
(ie. by using a vector<> and using operator[]), everything works just
fine.

							Mark A. Fox

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]