public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/30822]  New: wrong choice of overloaded template functions in recursive call
@ 2007-02-16 12:09 Zarathustra at gentlemansclub dot de
  2007-02-16 15:47 ` [Bug c++/30822] " pinskia at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Zarathustra at gentlemansclub dot de @ 2007-02-16 12:09 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-05-17 12:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-16 12:09 [Bug c++/30822] New: wrong choice of overloaded template functions in recursive call Zarathustra at gentlemansclub dot de
2007-02-16 15:47 ` [Bug c++/30822] " pinskia at gcc dot gnu dot org
2007-02-16 16:35 ` Zarathustra at gentlemansclub dot de
2007-02-21  7:44 ` Zarathustra at gentlemansclub dot de
2007-02-21 10:16 ` Zarathustra at gentlemansclub dot de
2007-03-09  4:14 ` bangerth at dealii dot org
2007-03-09 12:36 ` Zarathustra at gentlemansclub dot de
2007-05-17 12:27 ` Zarathustra at gentlemansclub dot de

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).