Boost MPL占位符和Lambda
问题描述:
我目前对boost :: mpl的概念样本做了一些证明,并且在理解lambda函数如何使用占位符时遇到了一些困难。Boost MPL占位符和Lambda
我意识到我可以将元函数封装在元函数类中,以使高阶函数能够访问嵌套的函数,并意识到您可以通过使用mpl :: lambda封装允许放置元函数持有人。
这实际上是如何工作的?我无法将头部缠绕在lamda和占位符实际上在封面下做的事情上。
答
查看Boost.MPL manual:占位符是mpl::arg<X>
形式的元函数类。元函数类是一个包含apply
元函数的类。
template <int N> struct arg; // forward declarations
struct void_;
template <>
struct arg<1>
{
template <
class A1, class A2 = void_, ... class Am = void_>
struct apply
{
typedef A1 type; // return the first argument
};
};
typedef arg<1> _1
这是mpl::lambda
将占位符表达式转换为元函数类的工作。这是通过嵌入元函数类等this完成:
template<
typename X
, typename Tag = unspecified
>
struct lambda
{
typedef unspecified type;
};
如果x是在一般的形式X<a1,...an>
,其中X
是类模板和a1,... an
是任意类型的一个占位符表达式,嵌入未指定类型f
相当于
typedef protect< bind<
quoten<X>
, lambda<a1>::type,... lambda<an>::type
> > f;
否则,f
相同X
。 apply
元函数通过访问嵌入类型来评估lambda表达式。
在MPL manual中,您还可以查找protect
,bind
和quote
的定义。他们都是围绕他们的论点,尽可能拖延评估。
清晰简明的答案 - 应该已被OP接受。 – etherice 2013-11-06 08:51:19
谢谢,很高兴得到了帮助! – TemplateRex 2013-11-07 10:42:37