boost::fusion::accumulate 简单的例子
boost::fusion::accumulate simple example
我希望这是一个简单的例子,有一个简单的问题,有人可以帮助我解决 :)。它基于另一个问题:
accumulate over tuple of values
我有一个结构,有一个 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 向我吐出的错误消息是:
required from here
/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
:
error: too many arguments to function
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()
。
我希望这是一个简单的例子,有一个简单的问题,有人可以帮助我解决 :)。它基于另一个问题: accumulate over tuple of values
我有一个结构,有一个 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 向我吐出的错误消息是:
required from here
/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
:
error: too many arguments to function
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()
。