Boost MPL: Call a (member) function only if it exists

拈花ヽ惹草 提交于 2019-12-06 02:44:25

问题


I have a class A that has a template parameter T. There are use cases where the class T offers a function func1() and there are use cases where T doesn't offer it. A function f() in A should call func1(), iff it exists. I think this should be possible with boost mpl, but I don't know how. Here some pseudo code:

template<class T>
class A
{
    void f(T param)
    {
        if(T::func1 is an existing function)
            param.func1();
    }
};

Even better would be an else-case:

template<class T>
class A
{
    void f(T param)
    {
        if(T::func1 is an existing function)
            param.func1();
        else
            cout << "func1 doesn't exist" << endl;
    }
};

回答1:


Boost.MPL doesn't deal with that as it's strictly for TMP and you can't call members in TMP. Boost.Fusion and Boost.TypeTraits don't have anything either; I thought one of them would but apparently I'm misremembering.

Here and here are some solutions on how to write a trait to detect a member in C++03. Once you have such a trait (I'll call it has_func1_member), you can use it for SFINAE:

template<typename T>
typename boost::enable_if<has_func1_member<T> >::type
maybe_call(T& t)
{ t.func1(); }

template<typename T>
typename boost::disable_if<has_func1_member<T> >::type
maybe_call(T&)
{
    // handle missing member case
}

// your example code would then do:
maybe_call(param);

Note that with C++11 it's easier to write the trait in the first place, although it's still somewhat arcane.



来源:https://stackoverflow.com/questions/7686753/boost-mpl-call-a-member-function-only-if-it-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!