TruLa TruLa - 2 months ago 19
C++ Question

c++11 metaprogramming: check for method existence

1) I have two classes

class A
and
class B
which both have a method called
foo
but with different argument lists.



class A {
public:
void foo(int a);
};

class B {
public:
void foo(int a, int b);
};


2) Further, I have a
class C
with template argument
T
that also has a
foo
method as following

template <typename T>
class C {
public:
void foo();
private:
T t_;
int a_;
int b_;
};


3) I would like use both
class A
and
class B
as template argument for
class C
.
Saying that I would like to have a method
C::foo
to be implemented like following:

template <typename T>
void C<T>::foo()
{
if (compile time check: T has foo(int a, int b))
t_.foo(a_, b_);
else
t_.foo(a_);
}


How can I express the
if
statement above in
C++11
?

Answer

Use SFINAE (with function template overloading).

template <typename T>
class C {
private:
    T t_;
    int a_;
    int b_;
public:
    template <typename X = T>
    auto foo() -> decltype (X().foo(a_)) {
        t_.foo(a_);
    }
    template <typename X = T>
    auto foo() -> decltype (X().foo(a_, b_)) {
        t_.foo(a_, b_);
    }
};

LIVE

Comments