gpt4 book ai didi

c++ - 解包后可变元组顺序根据数据类型发生变化

转载 作者:太空狗 更新时间:2023-10-29 21:39:28 24 4
gpt4 key购买 nike

代码应该通过从字符串中提取参数来回调函数。但是,顺序更改如下:(Visual Studio 2013 AND 2015!express)

"1 2 3 4"  int, double, string, int ->  3 2 4 1

"1 2 3 4" int, double, float, int -> 4 3 2 1

编辑:它works properly in gcc并且是 MS Visual C++ compiler bug - 针对 VS2013 和 VS2015 进行了测试。有谁知道解决方法? (也许使用一些 C++14 特性?)

Edit2:我解决了它向参数添加索引并删除元组的问题 http://cpp.sh/9jc5

这里是示例:

void one(int i, double d, string s, int ii)
{
std::cout << "function one(" << i << ", " << d << ", " << s << ", " << ii << ");\n";
}

int main()
{
RegisterRPC<int, double, string, int>("test1", one);
DataSource* data = new DataSource("1 2 3 4");
functionarray["test1"](data);
}

以及完整的代码:

#include <stdlib.h>
#include <functional>
#include <tuple>
#include <map>
#include <iostream>
#include <istream>
#include <sstream>
#include <string>

// ------------- UTILITY---------------
template<int...> struct index_tuple{};

template<int I, typename IndexTuple, typename... Types>
struct make_indexes_impl;

template<int I, int... Indexes, typename T, typename ... Types>
struct make_indexes_impl<I, index_tuple<Indexes...>, T, Types...>
{
typedef typename make_indexes_impl<I + 1, index_tuple<Indexes..., I>, Types...>::type type;
};

template<int I, int... Indexes>
struct make_indexes_impl<I, index_tuple<Indexes...> >
{
typedef index_tuple<Indexes...> type;
};

template<typename ... Types>
struct make_indexes : make_indexes_impl<0, index_tuple<>, Types...>
{};

// ----------UNPACK TUPLE AND APPLY TO FUNCTION ---------

using namespace std;

template<class Ret, class... Args, int... Indexes >
Ret apply_helper(Ret(*pf)(Args...), index_tuple< Indexes... >, tuple<Args...>&& tup)
{
return pf(forward<Args>(get<Indexes>(tup))...);
}

template<class Ret, class ... Args>
Ret apply(Ret(*pf)(Args...), const tuple<Args...>& tup)
{
return apply_helper(pf, typename make_indexes<Args...>::type(), tuple<Args...>(tup));
}

template<class Ret, class ... Args>
Ret apply(Ret(*pf)(Args...), tuple<Args...>&& tup)
{
return apply_helper(pf, typename make_indexes<Args...>::type(), forward<tuple<Args...>>(tup));
}
// --- make tuple ---

template <typename T> T read(std::istream& is)
{
T t; is >> t; cout << t << endl; return t;
}

template <typename... Args>
std::tuple<Args...> parse(std::istream& is)
{
return std::make_tuple(read<Args>(is)...);
}

template <typename... Args>
std::tuple<Args...> parse(const std::string& str)
{
std::istringstream ips(str);
return parse<Args...>(ips);
};

// ---- RPC stuff

class DataSource
{
std::string data;
public:
DataSource(std::string s) { data = s; };
template<class...Ts> std::tuple<Ts...> get() { return parse<Ts...>(data); };
};

std::map<std::string, std::function<void(DataSource*)> > functionarray;

template<typename... Args, class F>
void RegisterRPC(std::string name, F f) {
functionarray[name] = [f](DataSource* data){apply(f, data->get<Args...>()); };
}

// --------------------- TEST ------------------

void one(int i, double d, string s, int ii)
{
std::cout << "function one(" << i << ", " << d << ", " << s << ", " << ii << ");\n";
}

int main()
{
RegisterRPC<int, double, string, int>("test1", one);
DataSource* data=new DataSource("1 2 3 4");
functionarray["test1"](data);
system("pause");
return 0;
}

// --------------------- TEST ------------------

使用的引用资料

How do I expand a tuple into variadic template function's arguments?

build tuple using variadic templates

最佳答案

改变:

template <typename... Args>
std::tuple<Args...> parse(std::istream& is)
{
return std::make_tuple(read<Args>(is)...);
}

进入:

template <typename... Args>
std::tuple<Args...> parse(std::istream& is)
{
return std::tuple<Args...>{ read<Args>(is)... };
}

否则,未指定函数 read 的调用顺序。

关于c++ - 解包后可变元组顺序根据数据类型发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32914361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com