將std :: vector初始狀態應用boost :: fusion :: fold可以正常工作。使用fusion :: vector初始化狀態摺疊
#include <vector>
#include <boost/fusion/container/map.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/fold.hpp>
struct A;
struct B;
struct C;
int ia = 1, ib = 2, ic = 3;
namespace fusion = boost::fusion;
template< typename StdInteratorT>
struct initialize_map_with_std_vector
{
template<class T>
StdInteratorT operator()(StdInteratorT& i, T& val) {
val = *i;
return ++i;
}
};
void use_fold_with_std_vector()
{
using M = fusion::map<fusion::pair<A, int>, fusion::pair<B, char>, fusion::pair<C, double>>;
M fm;
std::vector<int> sv = { ia, ib, ic };
auto state = sv.begin();
fusion::fold(fm, state, initialize_map_with_std_vector<std::vector<int>::iterator>());
}
int main() { use_fold_with_std_vector(); }
當同樣的概念應用到融合::但是向量,代碼失敗,出現錯誤編譯 no instance of overloaded function "boost::fusion::fold" matches the argument
#include <vector>
#include <boost/fusion/container/map.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/fold.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
namespace fusion = boost::fusion;
struct A;
struct B;
struct C;
std::vector<int> va = { 4, 5, 6 };
std::vector<char> vb = { 'a', 'b', 'c' };
std::vector<double> vc = { 10., 11., 12. };
template< typename FusionIteratorT, typename Ret>
struct initialize_map_with_fusion_vector {
template<typename T>
Ret operator()(FusionIteratorT& i, T& val) {
val = fusion::deref(i);
return fusion::next(i);
}
};
void use_fold_with_fusion_vector() {
using M = fusion::map<fusion::pair<A, std::vector<int>>, fusion::pair<B, std::vector<char>>, fusion::pair<C, std::vector<double>>>;
using V = fusion::vector<std::vector<int>, std::vector<char>, std::vector<double>>;
auto fm = M();
auto fv = V(va, vb, vc);
using FusionIteratorT = decltype(fusion::begin(fv));
using Ret = fusion::result_of::next<fusion::result_of::begin<decltype(fv)>>;
fusion::fold(fm, fusion::begin(fv),
initialize_map_with_fusion_vector<FusionIteratorT, Ret >());
}
int main() {use_fold_with_fusion_vector();}
如何讓融合::倍工作與融合::矢量初始值設定項?
你的第一個示例代碼不編譯http://coliru.stacked-crooked.com/a/eb2d41fef08642ab –
道歉。複製粘貼錯誤我已更正此錯誤。 – hhbilly
http://coliru.stacked-crooked.com/a/490dd711ec829912 – hhbilly