Thread: More explicit template member function hell

  1. #1
    Registered User
    Join Date
    Apr 2007

    More explicit template member function hell

    For some reason with gcc 4.3 I can not solve the problem of explicit template member function instantiation. C++ restrictions and some compiler quirks have thwarted my efforts.

    Here is my code snippet,
    /* dummy class for function template recursion */
    template<int n>
    class ordn {
    ordn<n-1> sub1()
    ordn<n-1> o1 ;
    return(o1) ;
    ordn<n+1> add1()
    ordn<n+1> o1 ;
    return(o1) ;
    } ;
    template<int nrows, int ld>
    class cupper {
    /*  lots of code */
    /* urows template variable is a kludge since in fact urows = nrows in all cases */
        template <int urows, typename T >
            inline void invrec(T &on)
              cupper<urows-1,ld>  rsub(subptr()) ;
               /*   some code  */
              // rsub.invrec<urows-1>(o1) ;  
    /* Error occurs on next line.  gcc can't insantiate it even though all parameters
    are completely specified and in fact are template variables! */
              rsub.invrec<urows-1, ordn<urows-1> >(on.sub1()) ;  /* recursive inversion of submatrix */
    /* trick to obtain full function member specialization of a templated member 
    function */
        template<int urows>
            void invrec(ordn<1> &o1)
              cuComplex dd = this->getval(0,0) ;
              float c[2] ;
              c[0] = ((float) 1.0)/dd.x;
              c[1] = 0.0 ;
              this->setval(c,0,0) ;
    /* wrapper function to invoke the recursion */
         void inv()
                    ordn<nrows> on ;
                    invrec<nrows, ordn<nrows> >(on) ;
    } ; // end cupper
    Later I instantiate cupper of size <4,4> . Unfortunately I get an error from gcc of the form:
    error: no matching function for call to ‘cudamat::cupper<3, 4>::invrec(ordn<3>)’

    There are a number of C++ restrictions that conspire to make this difficult. The first problem is that we are forbidden to explicitly instantiate all template parameters inside the class for a class member. (Though supposedly you can do this for an inner class if it's enclosed in a local namespace).

    The second is that if you try to pull the definition out of the class definition, you run into the problem that partial specialization of function templates are not allowed. I'd have to specialize the entire class! (which is too large and complicated to consider).

    Any ideas or workarounds for this?

  2. #2
    Registered User
    Join Date
    Apr 2007
    OK the only way I can get this to work is to throw out function templates all together for template recursion and go strictly with function overloading.

    The overloaded function is a member of a templated class.

    /* same as above only more basic */
    template<bool b>
    class IsTrue {
    const static bool btype = b ;
     } ;
    typedef IsTrue<true>  TypeTrue ;
    typedef IsTrue<false> TypeFalse ;
    template <typename T, int iter>
    class dummy
    static void func(TypeTrue &tt)
    // end case specialization
    static void func(TypeFalse &tt)
    // standard case
    } ;
    /* a pseudo example */
    IsTrue<n == 1> it ;
    dummy<int, n>::func(it) ;
    In my code dummy is either an external template or the actual class of interest (cupper). It's sad that C++ forces these arcane workarounds. Hopefully the standards committee will relax full specialization of member functions or allow partial specialization of template functions. Either of these would help relieve the crappy syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM