从std::function复制模板参数
Copying template arguments from std::function
我想创建一个模板化类,其变量模板参数与给定的std::function
相同。例如
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
template <template<typename... TArgs> class T>
using ArgsCopy = T<Args...>;
};
template <typename... Args>
class _forwarder {
public:
using cb = std::function<void(Args...)>;
void operator()(Args... args) {
my_cb(args...);
}
private:
cb my_cb;
};
template <typename T>
using Forwarder = function_traits<T>::ArgsCopy<_forwarder>;
我将这样使用这个类
using cb_test = std::function<void(int, float, std::string)>;
Forwarder<cb_test> fwd;
fwd(5, 3.2, "hello");
Visual Studio抛出编译错误:error C2061: syntax error: identifier ' argscope '。我该如何解决这个问题?
微软的编译器没有完全遵循这里的标准,添加template
应该可以工作:
template <typename T>
using Forwarder = function_traits<T>::template ArgsCopy<_forwarder>;
但是如果你想让你的代码是可移植的,添加typename
:
template <typename T>
using Forwarder = typename function_traits<T>::template ArgsCopy<_forwarder>;
相关文章:
- 为什么 std::function 可以作为 std::not2 的参数?
- 传递给std::function template的template参数究竟代表什么
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 可变参数模板参数扩展 类型为 std::function 的类成员
- std::function<void()> 接受参数
- 在标准中,模板参数的语法在哪里定义,例如,'std::function<int(char)>'?
- C++派生类重载函数(带有 std::function 参数)不可见
- C++ 事件管理器的回调,使用 std::function 和 std:bind 以及派生类作为参数
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- 仅当一个参数中未使用 std::function 时,模板函数替换才有效
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 将另一个类的成员函数传递到 std::function 参数中
- 如何返回用参数包定义的成员函数的std::function对象
- 了解 std::function 的晦涩模板参数
- 将 std::function* 传递给模板参数
- 构造函数模板参数推导,其中 std::function 作为参数
- 使用"std::function"和先前推断的模板参数替换失败 - 为什么?
- 使用 std::function 作为具有任意数量参数的数据成员
- 将可变参数函数参数转发到 std::function 对象