Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么';这是等效的吗?_C++_Boost_Boost Phoenix - Fatal编程技术网

C++ 什么';这是等效的吗?

C++ 什么';这是等效的吗?,c++,boost,boost-phoenix,C++,Boost,Boost Phoenix,我有以下资料: class Foo { public: std::string const& Value() const { return /*Return some string*/; } }; typedef std::list<Foo> FooList; FooList foos; // Assume it has some valid entities inside std::vector<int> ints; FooList::const_i

我有以下资料:

class Foo
{
public:
   std::string const& Value() const { return /*Return some string*/; }
};

typedef std::list<Foo> FooList;
FooList foos; // Assume it has some valid entities inside

std::vector<int> ints;

FooList::const_iterator it, iend = foos.end();
for (it = foos.begin(); it != iend; ++it)
{
   ints.push_back(boost::lexical_cast<int>(it->Value()));
}
class-Foo
{
公众:
std::string const&Value()const{return/*返回一些字符串*/;}
};
typedef std::列表傻瓜;
愚蠢的傻瓜;//假设它里面有一些有效的实体
std::向量整数;
傻瓜:const_迭代器it,iend=foos.end();
for(it=foos.begin();it!=iend;++it)
{
ints.push_back(boost::lexical_cast(it->Value());
}
如何使用
std::for_each
boost::phoenix
实现for循环?我尝试了几种方法,但效果非常糟糕(我有大量嵌套的
bind()
语句)。我基本上只是想看看boost phoenix是如何将这一点转化为for循环的,所以我不会编写太多样板代码来迭代具有1-2行专门逻辑的容器


有时,执行lambdas pre-C++11似乎太难阅读和维护,不值得费心。

假设您准备了一个Phoenix友好的函数对象:

namespace lexical_casts
{
    template <typename T> struct to_
    {
        template <typename/*V*/> struct result { typedef T type; };
        template <typename V>
        T operator()(V const& v) const { return boost::lexical_cast<T>(v); }
    };

    boost::phoenix::function<to_<int> > to_int;
}
namespace词法转换
{
模板结构到_
{
模板结构结果{typedef T type;};
模板
T运算符()(V const&V)const{return boost::lexical_cast(V);}
};
boost::phoenix::函数到_int;
}
你可以这样写:

BOOST_AUTO(value_of, phx::lambda[ phx::bind(&Foo::Value, arg1) ]);

std::vector<int> ints;
boost::transform(
        foolist,
        back_inserter(ints), 
        lexical_casts::to_int(value_of(arg1)));
BOOST_AUTO(value_of,phx::lambda[phx::bind(&Foo::value,arg1)];
std::向量整数;
boost::transform(
傻瓜,
背面插入器(ints),
词法强制转换::to_int(arg1的值);

查看

我不熟悉嵌套结果结构的语法。您遗漏了模板参数名称,这是打字错误吗?另外,嵌套结构的用途是什么?你的解决方案看起来很干净@RobertDailey嵌套的typedef是,它通常用于Phoenix friendly(另请参见文档)。谢谢,您可以修改您的解决方案使其在C++11之前工作吗?我不会有新的
auto
关键字。@RobertDailey当然,这里是: