调用一个模板化函数不起作用
Calling one templated function isn't working
我不知道如何调用函数call
它是一个模板化的类,带有一个模板化的函数调用。但是我怎么使用这个代码呢?
#include <iostream>
#include <conio.h>
#include <functional>
template <typename Result> class Imp {
template <typename ...Args> int call(std::function<Result(Args...)> func, Args... args) {
return 0;
}
};
int f(double a, double b) {
return (int)a+b;
}
int main() {
Imp<int> a;
a.call<double, double>(f, 1., 1.); //!
}
error C2784: 'int Imp<int>::call(std::function<Result(Args...)>,Args...)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
with
[
Result=int
]
: see declaration of 'Imp<int>::call'
你不能像这样把一个function pointer
传递给std::function
(见这个问题)
改成这样:
template <typename Result> class Imp {
public:
template <class Func,typename ...Args> int call(Func f, Args... args) {
return 0;
}
};
int f(double a, double b) {return (int)a+b;}
int main() {
Imp<int> a;
a.call(f, 1., 1.); //!
}
ideone
或:
#include <functional>
template <typename Result> class Imp {
public:
template <typename ...Args> int call(std::function<Result(Args...)> f, Args... args) {
return 0;
}
};
int f(double a, double b) {return (int)a+b;}
int main() {
Imp<int> a;
a.call(std::function<int(double,double)>(f), 1., 1.); //!
}
相关文章:
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么我的数组双精度函数不起作用?
- std::函数不起作用,但普通的旧函数指针可以 - 为什么?
- C++ Arduino - 随机函数不起作用
- 为什么我的数组或函数不起作用?
- 从类中的另一个文件请求函数不起作用
- 双链表堆栈删除函数不起作用
- 将 cmake 代码段转换为函数 - 不起作用
- 为什么我的从base64解码的函数不起作用?
- 为什么当我们使用等于'='符号比较器函数时,c ++的内置排序函数不起作用?
- swap(int&, int&) 函数不起作用,当交换不使用临时变量时?
- 子类化 STL 容器:范围构造函数不起作用
- 带有此指针的模板类多重继承构造函数不起作用?
- 以下打印树函数不起作用为什么?
- 为什么内存集函数不起作用?
- 调用填充我的主体数组的函数不起作用
- 带有自定义数字的阶乘函数不起作用
- 如何QDialog raise()函数不起作用
- Lambda 表达式闭包函数不起作用
- C++:在没有 lvalue 的情况下调用时复制构造函数不起作用