Hello, in the attached piece of code two pairs of template functions are
defined which differ just in the order of their definitions.
The first pair (for_all_1) is accepted by the compiler (gcc 4.1.1) the second
(for_all_2) is rejected. (The aim of the code is to iterate over a list of
elements of different types and execute an operation on each element.) The code
compiles with the Visual Studio 2005 c++ compiler. gcc 3.3.1 claims that the
function calls are ambiguous. 

I do not have the newest gcc compiler. Could some one please confirm that this
is a bug and that it is not fixed with gcc 4.1.2 or later?

Thanks 
  Volker

#include <iostream>

class element
{
public:
 void test() { std::cout << "Hello World" << std::endl; }
};

template<typename TElement>
class op
{
public:
  void operator()(TElement elem) { elem.test(); }
};

class cons_end
{
};

template<typename TElement,typename TTail>
class cons
{
public:
 TElement elem;
 TTail tail;
};


// note the order of the definitions of the two functions named for_all_1
template<template<typename> class TOperator,typename TElement>
void for_all_1(TElement elem, cons_end tail)
{
  TOperator<TElement>()(elem);
}

template<template<typename> class TOperator,typename TElement, typename TTail>
void for_all_1(TElement elem, TTail tail)
{
  TOperator<TElement>()(elem);
  for_all_1<TOperator>(tail.elem,tail.tail);
}

// now the order changed and the compiler complains!
template<template<typename> class TOperator,typename TElement, typename TTail>
void for_all_2(TElement elem, TTail tail)
{
  TOperator<TElement>()(elem);
  for_all_2<TOperator>(tail.elem,tail.tail);
}

template<template<typename> class TOperator,typename TElement>
void for_all_2(TElement elem, cons_end tail)
{
  TOperator<TElement>()(elem);
}

int main(int argc, char *argv[])
{
  cons<element,cons<element,cons_end> > list;
  for_all_1<op>(list.elem,list.tail);
  for_all_2<op>(list.elem,list.tail);
}


-- 
           Summary: wrong choice of overloaded template functions in
                    recursive call
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Zarathustra at gentlemansclub dot de


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

Reply via email to