Problem with overloading function templates

From: Hendrik Schober (SpamTrap_at_gmx.de)
Date: 04/30/04


Date: Fri, 30 Apr 2004 16:28:28 +0200

Hi,

I have a problem, that boils down to the following code:

    #include <iostream>
    #include <typeinfo>

    class Test1 {};
    class Test2 {};
    class Test3 {};

    template< typename T >
    struct Cont { T t; };

    template< typename T >
    class TT { public: TT(T) {} };

    struct X {

      template< template<typename> class TT, typename T >
      static void f(const T& t)
      {
        std::cout << '\t' << typeid(t).name() << std::endl;
      }

      template< template<typename> class TT >
      static void f(const Test3& t)
      {
        std::cout << '\t' << typeid(t).name() << std::endl;
      }

      template< template<typename> class TT, typename T >
      static void f(const Cont<T>& c)
      {
        f<TT>( c.t ); // <== error here
      }

    };

    int main()
    {
      Cont<Test1> c1;
      Cont<Test2> c2;
      Cont<Test3> c3;

      X::f< TT, Test1 >(c1);
      X::f< TT, Test2 >(c2);
      X::f< TT, Test3 >(c3);

      return 0;
    }

For this Comeau and CW9 report (in the line marked
"error here") that the call is ambigous. (VC7.1
accepts the code, BTW.)
First, I don't see what's wrong, but overloading
rules, especially in conjunctions with templates,
are too complicated for me to remember them anyway.
Second, I need a solution. I'm sure I would get it
to work if I would use a class template and partial
specialize it. However, the original code is long
and complex enough without yet another helper class
template...
I would hope that, once the problem is known, a
simpler way could be found. AFAIK, function template
overloading _is_ possible, after all.

TIA,

Schobi

-- 
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
  Scott Meyers


Relevant Pages

  • Re: LoadString overwrite
    ... doing some special string replacements. ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ...
    (microsoft.public.vc.mfc)
  • Re: LoadString overwrite
    ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ... You might try suppressing the instantiation using the "extern template" ... play linker games to replace member functions. ...
    (microsoft.public.vc.mfc)
  • LoadString overwrite
    ... doing some special string replacements. ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ...
    (microsoft.public.vc.mfc)
  • Re: generic programming is very powerful. c should support it
    ... If you make the template parameters part of the function's name, ... If there is a fully specialized template for these parameters, ... from interactions with overloading and inheritance. ...
    (comp.lang.c)
  • Re: Help: How many explicit specializations required?
    ... > non template functions are overloadable ... specialization of the primary template. ... I think the above case is a partial ordering (overloading of the ... instantiation will be entered into the list of candidate functions. ...
    (comp.lang.cpp)