I am attempting to simulate the consequence of virtual friend function using a protected virtual member function (http://www.parashift.com/c++-faq-lite/buddies.html#faq-14.3). Additionally, I personally use explicit areas of expertise for my templated classes. I'm wondering if somebody could shed some light to know what I am doing wrong. Below, I am posting the code using the compiler errors. Any suggestions are welcomed!

foo.h

template < class T, class U >
class CFoo;

template < class T, class U >
void applyOnBar( const CFoo<T, U> &, const CBar<U> );

template < class T, class U >
class CFoo{

    public:
        .....
        friend void applyOnBar< >( const CFoo<T, U> &, const CBar<U> & ); // error, line 28: function does not match any template declaration.
        .....
        virtual ~CFoo();

    protected:

        virtual void do_applyOnBar(const CBar<U> &);
        .....
};

foo.cpp

template < class T, class U >
void applyOnBar( const CFoo<T, U> & refFoo, const CBar<U> & refBar ){
    refFoo.do_applyOnBar(refBar); // error, line 36: passing argument first argument discards qualifiers.
}

template < class T, class U >
void CFoo<T, U>::do_applyOnBar( const CBar<U> & refBar ){  // error, line 40: member function is protected.
    ......
}
#include "../impl/foo-impl.inc"

foo-impl.corporation

template class CFoo<float, float>; // line #1
.....
template void applyOnBar(const CFoo<float, float> &, const CBar<float> &); // line #6

./inc/foo.h: In instantiation of ‘CFoo<float, float>’:  
./src/../impl/foo-impl.inc:1:   instantiated from here  
./inc/foo.h:28: error: template-id ‘applyOnBar<>’ for ‘void applyOnBar(const CFoo<float, float>&, const CBar<float>&)’ does not match any template declaration  

./src/foo.cpp: In function ‘void applyOnBar(const CFoo<T, U>&, const CBar<U>&) [with T = float, U = float]’:  
./src/../impl/foo-impl.inc:6:   instantiated from here  
./src/foo.cpp:40: error: ‘void CFoo<T, U>::do_applyOnBar(const CBar<U>&) [with T = float, U = float]’ is protected  
./src/foo.cpp:36: error: within this context  
./src/foo.cpp:36: error: passing ‘const CFoo<float, float>’ as ‘this’ argument of ‘void CFoo<T, U>::do_applyOnBar(const CBar<U>&) [with T = float, U = float]’ discards qualifiers
refFoo.do_applyOnBar(refBar); // error

Since refFoo is really a mention of the const CFoo, therefore it cannot be employed to call non-const function. do_applyOnBar is really a non-const member function.

So either you are making d_applyOnBar a const-function, by writing the keyword const around the right side of function signature, or make refFoo non-const by getting rid of the const qualifier within the parameter declaration!

Another issue is stated by Erik. See his answer!

template < class T, class U > void applyOnBar( const CFoo<T, U> &, const CBar<U> );

That lacks an &lifier around the second argument, ought to be:

template < class T, class U > void applyOnBar( const CFoo<T, U> &, const CBar<U> & );

Next,

virtual void do_applyOnBar(const CBar<U> &);

That one requires a const, because you use refFoo.do_applyOnBar(refBar); with refFoo being const.

virtual void do_applyOnBar(const CBar<U> &) const;