Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.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++中的变量函数允许用户调用任意数量的函数。例如,sscanf将要解析的字符串、格式字符串和一组参数作为输入,这些参数将获取解析项的值。 如果我想让这个范例异步呢?我需要解析一些数据,从一些字节中提取可变数量的参数。需要提取的参数以sscanf中的格式字符串指定。我希望我的函数被这样调用: function <void (int, int, int)> myfunc = [=] (int a, int b, int c) { // Do something here! }; asyncsscanf(my_bytes, "iii", myfunc); 函数myfunc=[=](int a,int b,int c) { //在这里做点什么! }; asyncscanf(my_字节,“iii”,myfunc);_C++_Callback_Variadic Templates_Variadic - Fatal编程技术网

C++;可变回调 C++中的变量函数允许用户调用任意数量的函数。例如,sscanf将要解析的字符串、格式字符串和一组参数作为输入,这些参数将获取解析项的值。 如果我想让这个范例异步呢?我需要解析一些数据,从一些字节中提取可变数量的参数。需要提取的参数以sscanf中的格式字符串指定。我希望我的函数被这样调用: function <void (int, int, int)> myfunc = [=] (int a, int b, int c) { // Do something here! }; asyncsscanf(my_bytes, "iii", myfunc); 函数myfunc=[=](int a,int b,int c) { //在这里做点什么! }; asyncscanf(my_字节,“iii”,myfunc);

C++;可变回调 C++中的变量函数允许用户调用任意数量的函数。例如,sscanf将要解析的字符串、格式字符串和一组参数作为输入,这些参数将获取解析项的值。 如果我想让这个范例异步呢?我需要解析一些数据,从一些字节中提取可变数量的参数。需要提取的参数以sscanf中的格式字符串指定。我希望我的函数被这样调用: function <void (int, int, int)> myfunc = [=] (int a, int b, int c) { // Do something here! }; asyncsscanf(my_bytes, "iii", myfunc); 函数myfunc=[=](int a,int b,int c) { //在这里做点什么! }; asyncscanf(my_字节,“iii”,myfunc);,c++,callback,variadic-templates,variadic,C++,Callback,Variadic Templates,Variadic,asyncsscanf应该完成所需的处理,完成后,我希望它使用格式字符串中指定的正确参数调用myfunc。 有可能做这样的事吗 谢谢你我不知道用你的方法怎么做 首先,asyncscanf函数的第三个参数没有合适的类型。asyncscanf正文中唯一可接受的参数是void(…)(函数接受无限数量的参数,但不返回任何内容),但作为第三个参数传递给asyncscanf的参数应该是可能不可接受的类型 其次,您必须根据格式(在示例中为“iii”)对my_字节进行争用。如果您有有限数量的不同格式字符串(然后

asyncsscanf应该完成所需的处理,完成后,我希望它使用格式字符串中指定的正确参数调用myfunc。 有可能做这样的事吗


谢谢你

我不知道用你的方法怎么做

首先,asyncscanf函数的第三个参数没有合适的类型。asyncscanf正文中唯一可接受的参数是void(…)(函数接受无限数量的参数,但不返回任何内容),但作为第三个参数传递给asyncscanf的参数应该是可能不可接受的类型

其次,您必须根据格式(在示例中为“iii”)对my_字节进行争用。如果您有有限数量的不同格式字符串(然后您可以“切换”所有可能的格式),则可以这样做。但在一般情况下,我认为这是不可能做到的

但是因为你用“可变模板”来标记你的问题,我假设你使用的是C++11/14。 也许您希望将asyncscanf的format参数设置为更具可读性的模板参数(我假设在编译时格式总是已知的)。下面是一段解决方案

#include <functional>
#include <iostream>

// template parsing function, remaining_bytes parameter should contain pointer to not parsed part
// of my_bytes
template <typename return_type> return_type parse(const char *my_bytes, const char *&remaining_bytes);

// specialization of parsing function for desired types, fake implementation
template <> int parse<int>(const char *my_bytes, const char *&remaining_bytes) {
    remaining_bytes = my_bytes;
    return 0;
}

// specialization of parsing function for desired types, fake implementation
template <> long parse<long>(const char *my_bytes, const char *&remaining_bytes) {
    remaining_bytes = my_bytes;
    return 1;
}

// declare helper template for general case
template <typename to_be_parsed_tuple, typename parsed_tuple>
struct asyncscanf_helper;

// all params parsed case
template <typename... parsed_params>
struct asyncscanf_helper<std::tuple<>, std::tuple<parsed_params...>> {
    void operator()(const char *, std::function<void(parsed_params...)> fun, parsed_params... params) {
        fun(params...);
    }
};

// some params to be parsed case
template <typename first_param_to_be_parsed, typename...to_be_parsed_params, typename... parsed_params>
struct asyncscanf_helper<std::tuple<first_param_to_be_parsed, to_be_parsed_params...>, std::tuple<parsed_params...>> {
    void operator()(const char *my_bytes, std::function<void(parsed_params..., first_param_to_be_parsed, to_be_parsed_params...)> fun, parsed_params... params) {
        const char *remaining_bytes = 0;
        first_param_to_be_parsed p1 = parse<first_param_to_be_parsed>(my_bytes, remaining_bytes);
        asyncscanf_helper<std::tuple<to_be_parsed_params...>, std::tuple<parsed_params..., first_param_to_be_parsed>>()(remaining_bytes, fun, params..., p1);
    }
};

template <typename... params>
void asyncscanf(const char *my_bytes, void function(params...)) {
    asyncscanf_helper<std::tuple<params...>, std::tuple<>>()(my_bytes, function);
}

void test_fun(int a, int b, int c) {
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
}

void test_fun2(int a, long b, int c) {
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
}

int main() {
    asyncscanf("1 2 3", test_fun);
    asyncscanf("1 2 3", test_fun2);
}
#包括
#包括
//模板解析函数,剩余的_字节参数应包含指向未解析部分的指针
//我的字节数
模板返回类型解析(常量字符*我的字节,常量字符*&剩余的字节);
//为所需类型专门化解析函数,伪实现
模板int解析(常量字符*我的字节,常量字符*&剩余的字节){
剩余的_字节=我的_字节;
返回0;
}
//为所需类型专门化解析函数,伪实现
模板长解析(常量字符*我的字节,常量字符*&剩余的字节){
剩余的_字节=我的_字节;
返回1;
}
//为一般情况声明帮助器模板
模板
结构异步扫描辅助程序;
//解析的所有参数大小写
模板
结构异步扫描辅助程序{
void运算符(){
乐趣(params…);
}
};
//要分析的某些参数的大小写
模板
结构异步扫描辅助程序{
void操作符()(const char*my_bytes,std::function fun,parsed_params…params){
常量字符*剩余字节=0;
第一个要被解析的参数p1=parse(my_字节,剩余的_字节);
asyncscanf_helper()(剩余的_字节,fun,参数…,p1);
}
};
模板
void asyncscanf(常量字符*我的字节,void函数(参数…){
asyncscanf\u helper()(我的字节,函数);
}
无效测试(内部a、内部b、内部c){

我不知道用你的方法怎么做

首先,asyncscanf函数的第三个参数没有合适的类型。只有在asyncscanf主体中可以接受的类型是void(…)(函数接受无限数量的参数,并且不返回任何内容),但作为第三个参数传递给asyncscanf的参数应该是可能不可接受的类型

第二,您必须根据格式(在示例中为“iii”)对my_字节进行Dispatch。如果您有有限数量的不同格式字符串,则可以这样做(然后您可以“切换”所有可能的格式)。但在一般情况下,我想这是不可能的

但是因为你用“可变模板”来标记你的问题,我假设你使用的是C++11/14。 也许您希望将asyncscanf的format参数设置为更具可读性的模板参数(我假设在编译时格式总是已知的)

#include <functional>
#include <iostream>

// template parsing function, remaining_bytes parameter should contain pointer to not parsed part
// of my_bytes
template <typename return_type> return_type parse(const char *my_bytes, const char *&remaining_bytes);

// specialization of parsing function for desired types, fake implementation
template <> int parse<int>(const char *my_bytes, const char *&remaining_bytes) {
    remaining_bytes = my_bytes;
    return 0;
}

// specialization of parsing function for desired types, fake implementation
template <> long parse<long>(const char *my_bytes, const char *&remaining_bytes) {
    remaining_bytes = my_bytes;
    return 1;
}

// declare helper template for general case
template <typename to_be_parsed_tuple, typename parsed_tuple>
struct asyncscanf_helper;

// all params parsed case
template <typename... parsed_params>
struct asyncscanf_helper<std::tuple<>, std::tuple<parsed_params...>> {
    void operator()(const char *, std::function<void(parsed_params...)> fun, parsed_params... params) {
        fun(params...);
    }
};

// some params to be parsed case
template <typename first_param_to_be_parsed, typename...to_be_parsed_params, typename... parsed_params>
struct asyncscanf_helper<std::tuple<first_param_to_be_parsed, to_be_parsed_params...>, std::tuple<parsed_params...>> {
    void operator()(const char *my_bytes, std::function<void(parsed_params..., first_param_to_be_parsed, to_be_parsed_params...)> fun, parsed_params... params) {
        const char *remaining_bytes = 0;
        first_param_to_be_parsed p1 = parse<first_param_to_be_parsed>(my_bytes, remaining_bytes);
        asyncscanf_helper<std::tuple<to_be_parsed_params...>, std::tuple<parsed_params..., first_param_to_be_parsed>>()(remaining_bytes, fun, params..., p1);
    }
};

template <typename... params>
void asyncscanf(const char *my_bytes, void function(params...)) {
    asyncscanf_helper<std::tuple<params...>, std::tuple<>>()(my_bytes, function);
}

void test_fun(int a, int b, int c) {
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
}

void test_fun2(int a, long b, int c) {
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
}

int main() {
    asyncscanf("1 2 3", test_fun);
    asyncscanf("1 2 3", test_fun2);
}
#包括
#包括
//模板解析函数,剩余的_字节参数应包含指向未解析部分的指针
//我的字节数
模板返回类型解析(常量字符*我的字节,常量字符*&剩余的字节);
//为所需类型专门化解析函数,伪实现
模板int解析(常量字符*我的字节,常量字符*&剩余的字节){
剩余的_字节=我的_字节;
返回0;
}
//为所需类型专门化解析函数,伪实现
模板长解析(常量字符*我的字节,常量字符*&剩余的字节){
剩余的_字节=我的_字节;
返回1;
}
//为一般情况声明帮助器模板
模板
结构异步扫描辅助程序;
//解析的所有参数大小写
模板
结构异步扫描辅助程序{
void运算符(){
乐趣(params…);
}
};
//要分析的某些参数的大小写
模板
结构异步扫描辅助程序{
void操作符()(const char*my_bytes,std::function fun,parsed_params…params){
常量字符*剩余字节=0;
第一个要被解析的参数p1=parse(my_字节,剩余的_字节);
asyncscanf_helper()(剩余的_字节,fun,参数…,p1);
}
};
模板
void asyncscanf(常量字符*我的字节,void函数(参数…){
asyncscanf\u helper()(我的字节,函数);
}
无效测试(内部a、内部b、内部c){

我不知道用你的方法怎么做

首先,asyncscanf函数的第三个参数没有合适的类型。只有在asyncscanf主体中可以接受的类型是void(…)(函数接受无限数量的参数,并且不返回任何内容),但作为第三个参数传递给asyncscanf的参数应该是可能不可接受的类型

其次,您必须根据格式(“iii”)对my_字节进行争用