如果我有类型,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 用于查找类型是否为元函数类的宏完全相同的宏。
#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/