对于Boost Proto表达式,什么时候不应该期望proto_tag
成员?我可以查询占位符的标记类型,例如,使用以下方法之一:
typedef proto::tag_of<decltype(_1)>::type ta;
typedef decltype(_1)::proto_tag tb;
但是,如果我询问表达式的子的标记类型,则似乎没有proto_tag
成员;下面代码的第三行给出了一个错误:
auto f = _1 + _2;
typedef proto::tag_of<decltype(proto::child_c<0>(f))>::type tc;
typedef decltype(proto::child_c<0>(f))::proto_tag td; // error
Clang和GCC的错误报告有问题的类型:不是类、名称空间或作用域枚举。我使用Clang 3.2, GCC 4.7.2和Boost 1.53。
g++ 4.8.0给出的错误基本上是:
decltype计算为
phx::actor<proto::expression>&
,它不是一个类或枚举类型
为了使用::
,你需要有一个不合格的类型,所以你必须删除引用:
#include <type_traits>
typedef std::remove_reference<decltype(proto::child_c<0>(f))>::type::proto_tag td;
我相信你也可以使用:
typedef proto::result_of::child_c<decltype(f),0>::type::proto_tag td;
使用proto::tag_of
,您不需要担心可能的引用,因为它在必要时被删除:
template<typename Expr>
struct tag_of
{
typedef typename Expr::proto_tag type;
};
template<typename Expr>
struct tag_of<Expr &>
{
typedef typename Expr::proto_tag type;
};