On Apr 23, 11:00*am, DeMarcus <use_my_alias_h...@hotmail.com> wrote:
> Hi,
>
> I have a function template structure like this.
>
> struct A
> {
>
> };
>
> struct B : A
> {
>
> }
>
> class SomeClass
> {
> public:
>
> * * template<typename T>
> * * void fnc( const T& t )
> * * {
> * * * *std::cout << "Template" << std::endl;
> * * }
>
> * * void fnc( const A& a )
> * * {
> * * * *std::cout << "Non-template" << std::endl;
> * * }
>
> };
>
> int main()
> {
> * * SomeClass sc;
> * * sc.fnc( A() ); *// This gives me "Non-template".
> * * sc.fnc( B() ); *// Error! This gives me "Template"
> * * * * * * * * * * // even though B inherits from A.
>
> }
>
> What's the proper way making instances of B access the non-templated
> function?
>
> Thanks,
> Daniel
Use enable_if and make them both templates:
class SomeClass
{
public:
template<typename T>
void fnc( const T& t, typename enable_if<inherits<T, A> >::type *
dummy = 0 )
{
std::cout << "Inherits A" << std::endl;
}
template<typename T>
void fnc( const T& t )
{
std::cout << "Doesnt inherit A" << std::endl;
}
};
You can read about enable_if here:
http://www.boost.org/doc/libs/1_42_0...enable_if.html