如果我的类型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>>
{ };
我可以把什么放进???这样做我想要的呢?
答案 0 :(得分:2)
免责声明:我远不是MPL的专家,所以我不能保证这是解决这个问题的最佳方法(或者即使它是正确的,它似乎至少起作用)。
根据文档,mpl::apply
的第一个参数/参数需要是Lambda表达式,可以是元功能类或占位符表达式。快速谷歌搜索引导我this post。根据该帖子,mpl::is_lambda_expression
允许您确定类型是否是占位符表达式。在Boost.TTI(从版本1.54开始提升)中,你可以找到一个完全符合你想要的元函数。此元函数为boost::tti::detail::is_lambda_expression
,可在boost/tti/detail/dlambda.hpp
中找到。在下面的示例中,我使用了与TTI用于查找类型是否为元功能类的完全相同的宏。
#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;
}