jeudi 28 janvier 2016

Detecing class member with specific signature

I have Class A with functions with different signatures. I would like to detect if specific signature exists and if it does call it and call some other more generic function if it doesn`t.

#include <iostream>
#include <type_traits>

template < class T >
struct has_fun_with_bool;

class B {};
class C {};
class D {};

class A
{
public:
    void fun (B b) { std::cout<<"B without bool " << std::endl; };
    void fun (B b, bool f) { std::cout<<"B with bool: " << f << std::endl; };
    void fun (C b) { std::cout<<" C without bool "<< std::endl; };
    void fun (D d ,bool f ) { std::cout<<"D with bool: "<< f <<std::endl;

    template <class T>
    void bar(T in, const bool b = false) {
        fun (in, b); or fun (in);
    }
};


int main()
{
    A a;
    B b;
    C c;
    D d;
    a.bar(b);
    a.bar(c);
    a.bar(d);

return 0;
}

For A bar should use fun with bool parameter, for C without and for D with.

Aucun commentaire:

Enregistrer un commentaire