【问题标题】:Boost MPL: Call a (member) function only if it existsBoost MPL:仅在存在时调用(成员)函数
【发布时间】:2011-10-07 11:43:34
【问题描述】:

我有一个具有模板参数 T 的类 A。在某些用例中,类 T 提供函数 func1(),而在某些用例中 T 不提供函数。 A 中的函数 f() 应该调用 func1(),如果它存在的话。我认为这应该可以通过 boost mpl 实现,但我不知道如何。 这里有一些伪代码:

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

如果是 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;
    }
};

【问题讨论】:

  • 即使您设法找到代码无法编译的适当条件。 if 的两个分支都必须编译,如果函数不存在,则 true 分支不会编译。
  • 使用模板特化,两个部分都不需要编译。不知何故,我必须调用带有参数 T 的模板函数,并且当 T 提供或不提供 func1() 时,此函数具有不同的行为。我确信 boost mpl 为这个用例提供了一些东西。我就是不知道怎么用。

标签: c++ boost sfinae template-meta-programming boost-mpl


【解决方案1】:

Boost.MPL 不处理这个问题,因为它只针对 TMP,您不能调用 TMP 中的成员。 Boost.Fusion 和 Boost.TypeTraits 也没有任何东西。我以为其中一个会,但显然我记错了。

Herehere 是一些关于如何在 C++03 中编写特征来检测成员的解决方案。一旦你有了这样的特征(我称之为has_func1_member),你就可以将它用于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);

请注意,使用 C++11,首先编写 trait 会更容易,尽管它仍然有些晦涩。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多