------- Comment #2 from pinskia at gcc dot gnu dot org  2006-02-02 18:47 -------
The issue here (in the source) is that the overloaded of
"    result += size(*iter);" is only the size functions above that call so it
does not see the template below that call which is the function you would like
to call.

This is how standard C++ works (with the correction from DR 197).

This is a dup of bug 2922 which was fixed by rejecting invalid code and fixing
wrong code for 4.1.

The way to fix the code is to add a forward to the template function.

So the following code is the legal corrected code:
#include <algorithm>
#include <string>
#include <vector>

int size(char x) { return (int) sizeof(x); }
int size(int x) { return (int) sizeof(x); }

int size(const std::string &x) {
  return (int) x.size() + (int) sizeof(int);
}

template <class T, class TT>
int size(const std::pair<T,TT> &x);

template <class T>
int size(const std::vector<T> &x) {
  int result = (int) sizeof(int);
  typename std::vector<T>::const_iterator iter;
  for (iter = x.begin() ; iter != x.end() ; iter++)
    result += size(*iter);
  return result;
}

template <class T, class TT>
int size(const std::pair<T,TT> &x) {
  return size(x.first) + size(x.second);
}

int foo() {
  std::vector<std::pair <std::string, std::string> > pvec;
  return size(pvec);
}

--------
The missing of the first template in the diangostic is just a diagnostic bug
which was reported somewhere else too, PR 16057.



*** This bug has been marked as a duplicate of 2922 ***

*** This bug has been marked as a duplicate of 2922 ***


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26079

Reply via email to