boost::fusion::积累简单的例子
boost::fusion::accumulate simple example
我有一个我希望是简单的例子,其中有一个简单的问题,有人可以帮我澄清:)。它基于另一个问题:累加值的元组
我有一个结构,有一个std::tuple<std::string>
成员。在结构的构造过程中,我在执行了由另一种类型控制的一些算法后存储数据。与我的问题无关,只是解释下面的代码。
我需要帮助的是我的toString()
方法,该方法调用我所拥有的元组上的boost::fusion::accumulate
。当我呼叫boost::fusion::accumulate
时,我不知道该怎么称呼它!对boost::fusion::accumulate
的调用应该由AlgorithmT
参数指定。
我或多或少有以下代码:
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/phoenix/phoenix.hpp>
template<typename AlgorithmT, typename ...T>
struct Trait
{
using A = AlgorithmT;
size_t hash_value_;
std::tuple<std::string> value_;
Trait(T... data) :
hash_value_(A::Hash(data...)),
value_(A::Transform(data...)) {}
size_t hash() const { return this->hash_value_; }
std::string toString() const
{
using namespace boost::phoenix::arg_names;
// This example compiles, but it effectively does nothing. result is always empty.
std::string const result = boost::fusion::accumulate(this->value_, std::string{}, arg1);
// This next line doesn't compile, but I think it's what I want.
std::string const result = boost::fusion::accumulate(this->value_, std::string{}, &A::ToString(arg1));
return result;
}
};
struct IdentityAlgorithms
{
static constexpr size_t (*Hash)(std::string const&) = &boost::hash_value;
static constexpr auto Transform = &identity_transform<std::string>;
static std::string ToString(std::string value) {
std::cerr << "ToString invoked! value: '" << value << "'" << std::endl;
return """ + value + """; }
};
有人能看看我是如何使用boost::fusion::accumulate
的吗?也许可以指出我如何让编译器推导类型。我假设编译器不能推断出正确的类型是有充分理由的,我只是不确定那是什么。GCC4.9向我吐出的错误消息是:
从这里开始需要
/local/brazil-pkg-cache/packages/Boost/Boost-3.0.3932.1/RHEL5_64/DEV.STD.PTHREAD/build/include/boost/fusion/algorithm/iteration/detail/preprocessed/fold.hpp
:111
:39
:
错误:参数太多,无法运行fusion::deref(it0));
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/phoenix/phoenix.hpp>
template<typename AlgorithmT, typename ...T>
struct Trait
{
using A = AlgorithmT;
size_t hash_value_;
std::tuple<std::string> value_;
Trait(T... data) :
hash_value_(A::Hash(data...)),
value_(A::Transform(data...)) {}
size_t hash() const { return this->hash_value_; }
std::string toString() const
{
using namespace boost::phoenix::arg_names;
const std::string result = boost::fusion::accumulate(this->value_, std::string{} /* initila state */, A());
return result;
}
};
struct IdentityAlgorithms
{
typedef std::string result_type;
static constexpr size_t (*Hash)(std::string const&) = &boost::hash_value;
static constexpr auto Transform = &identity_transform<std::string>;
std::string operator()(const std::string& str, const std::string &value) const
// ^ state ^ element of squence
{
std::cerr << "ToString invoked! value: '" << value << "'" << std::endl;
return """ + value + """;
}
};
在这种情况下,操作static std::string ToString(std::string value)
没有意义。您必须定义operator()
。
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- 显示错误输出的简单数组排序程序
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 退出简单while循环时出现问题
- 为什么简单的算术减法在"if"条件下不起作用?
- C++-字符串是否包含一个带有简单循环的单词
- 关于 c++ 函数中指针赋值的简单问题
- 从函数返回任意简单类型的数据
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- boost::fusion::积累简单的例子