0

I have compilation errors when I'm trying something simple with sfinae : I would like a template to declare functions depending on the encapsulated type. Thing is, I have the error "request for member ‘f’ in ‘c1’, which is of non-class type ‘C(A (*)())’ (...) "

I have a small code sample below :

struct A
{
    void f() { cout << "f" << endl; }   
    void g() { cout << "gA" << endl; }  
};

struct B
{
    void g() { cout << "gB" << endl; }
    void h() { cout << "h" << endl; }
};

template<class T>
struct C
{
    T t;
    C(T t) : t(t) { }

    auto f() -> decltype(t.f()) { t.f(); }
    auto h() -> decltype(t.h()) { t.h(); }

    void g() { t.g(); }
};

int main()
{
    C<A> c1(A());
    c1.f(); //f
    c1.g(); //gA
    //c1.h(); //should not compile

    C<B> c2(B());   
    //c2.f(); //should not compile
    c2.g(); //gB
    c2.h(); //h
}

I'm a beginner with this, so I'm not sure it is even "clean" to write the stuff like that, and I couldn't figure out how to make it work with similar posted problms. Help appréciated.

R. Absil
  • 173
  • 6

0 Answers0