c++ - 如何检查类型是否为 mpl::apply-able?

标签 c++ boost-mpl

如果我有类型,T ,看起来像 Foo<mpl::_1> ,我能做到mpl::apply<T, int>::type得到Foo<int> .

但是如果T是一个完整的类型,比如 Foo<int> , 然后 mpl::apply<T, int>::type不会编译。

我想编写一个元函数,如果可能的话将应用一个类型,否则返回该类型。所以像这样:

template <typename Partial, typename T>
struct maybe_apply
: eval_if_c<??????,
    mpl::apply<Partial, T>,
    mpl::identity<Partial>>
{ };

我可以在 ???s 中放什么,这样它就能达到我想要的效果?

最佳答案

免责声明:我远不是 MPL 方面的专家,所以我不能保证这是解决这个问题的最佳方法(或者即使它是正确的,它至少看起来是有效的)。

根据文档,mpl::apply 的第一个参数/参数需要是 Lambda 表达式,并且可以是元函数类或占位符表达式。快速的谷歌搜索让我找到了 this post .根据该帖子,mpl::is_lambda_expression 允许您确定类型是否为占位符表达式。在 Boost.TTI(从 1.54 版开始就在 boost 中)你可以找到一个元函数来做你想要的。这个元函数是 boost::tti::detail::is_lambda_expression,可以在 boost/tti/detail/dlambda.hpp 中找到。在下面的示例中,我使用了与 TTI 用于查找类型是否为元函数类的宏完全相同的宏。

Running on Coliru.

#include <iostream>
#include <typeinfo>

#include <boost/utility.hpp>

#include <boost/mpl/apply.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>

#include <boost/mpl/plus.hpp>

namespace mpl=boost::mpl;


/* //This is another way to do it
template <typename T, typename Enable=void>
struct is_apply_able : mpl::false_
{};

template <typename T>
struct is_apply_able<T,typename boost::enable_if<mpl::is_lambda_expression<T> >::type> : mpl::true_
{};
BOOST_MPL_HAS_XXX_TEMPLATE_NAMED_DEF(is_metafunction_class, apply, false)
template <typename T>
struct is_apply_able<T,typename boost::enable_if<is_metafunction_class<T> >::type> : mpl::true_
{};*/


BOOST_MPL_HAS_XXX_TEMPLATE_NAMED_DEF(is_metafunction_class, apply, false)

template <typename T>
struct is_apply_able : mpl::or_<is_metafunction_class<T>,mpl::is_lambda_expression<T> >
{};

struct plus_two
{
    template <typename Number>
    struct apply
    {
        typedef typename mpl::plus<Number,mpl::int_<2> >::type type;
    };
};

template <typename T>
struct Foo
{};

template <typename Partial, typename T>
struct maybe_apply
: mpl::eval_if<is_apply_able<Partial>,
    mpl::apply<Partial, T>,
    mpl::identity<Partial> > 
{ };

int main()
{
    std::cout << typeid(maybe_apply<Foo<mpl::_1>,int>::type).name() << std::endl;
    std::cout << typeid(maybe_apply<plus_two,mpl::int_<1> >::type).name() << std::endl;
    std::cout << typeid(maybe_apply<Foo<float>,int>::type).name() << std::endl;
}

关于c++ - 如何检查类型是否为 mpl::apply-able?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18541644/

相关文章:

c++ - 在 std 错误的命名空间中没有名为 'bind"的成员

c++ - 如何使用 Boost MPL 在函数中拥有多个返回点?

c++ - 是否有类似 MATLAB 的 OpenCV 颜色条的东西?

c++ - LIVE555 如何使用 h264 成帧器类获取 ffmpeg 的最终单位

c++ - 使用 boost::mpl::bitor_

c++ - 如何将 mpl::set 的内容扩展为函数模板的模板参数

c++ - 使用可变模板参数增强变体访问者

c++ - 如何在折叠期间获取成员类型的 boost::mpl 占位符

C++ STL : Passing an empty container to lower_bound

c++ - 如何使用 cin 获取字符串而不是对其进行硬编码?