IkoLogs
IkoLogs

Reputation: 59

How To Forward Declare Template instance member function

CRTP interface with template member function, has problem with calling implementation template member function, due to no forward declaration of template member function.

Class IF is interface , Class Src is Implementation.

    template<class T_Src>
    struct IF {


        template<class T>
        static void f1()
        {
            T_Src::template imp_f1<T>();
        }

template<class T>
    inline void f3()
    {       
        /*this line does not compile*/
        static_cast<T_Src*>(this)->imp_f3<T>();
    }
}


struct Src:public IF<Src>
{
    template<class T>
    static void imp_f1()
    {

    }



    template<class T>
    inline void imp_f3()
    {

    }

};

Works ok for static function interface implementation as in IF::f1

But for IF::f3 i get MSVC error

" C2760: syntax error: unexpected token ')', expected 'expression"

In Summary this works with global and static member template functions, but for class member template functions seems forward declaration is required.

Dont mind solution that includes using some extra template magic to get around this problem such as this

enter link description here

However that fix linked no work for me.

Currently trying to avoid my CRTP interface being limited by not being able to use instance template functions in the interface.

Cheers

Upvotes: 0

Views: 278

Answers (1)

sebrockm
sebrockm

Reputation: 6002

You have to add the template keyword before imp_f3<T>(), just as you did it for T_Src::template imp_f1<T>():

static_cast<T_Src*>(this)->template imp_f3<T>();

Have a look at this question for an explanation.

Upvotes: 2

Related Questions