在 Boost Proto 表达式中,我什么时候不应该期待 proto_tag
成员?例如,我可以使用以下任一方法查询占位符的标记类型:
typedef proto::tag_of<decltype(_1)>::type ta;
typedef decltype(_1)::proto_tag tb;
但是如果我询问表达式的 child 的标签类型,似乎没有 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 evaluates to
phx::actor<proto::expression>&
, which is not a class or enumeration type
为了使用::
你需要有一个不合格的类型,所以你必须删除引用:
#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;
};
关于c++ - 获取 Boost Proto 子表达式的标签类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16619048/