作为 std::function 包装器的构造函数的参数的函数
Function as an argument of a constructor of std::function wrapper
我正在编写 Monitor class 来解决同步问题,我想实现一个 'Entry' class 来包装 std::function。
我实现了一点,使用了函数特征,但现在我只能使用准备好的 std::function 对象构造 Entry 对象。尝试编写一个将普通函数作为参数的构造函数失败,并显示有关模板参数 deduction/substitution 和 参数的编译器消息。
程序运行正常,但我只是好奇如何实现给定的构造函数,这是我的代码:
template <class F>
struct FunctionType;
template <class R, class Object, class... Args>
struct FunctionType<R (Object::*)(Args...)> {
typedef R return_type;
};
template <class R, class Object, class... Args>
struct FunctionType<R (Object::*)(Args...) const> {
typedef R return_type;
};
template <class F> class Entry {
std::function<F> internalFunction;
...
public:
template <F> Entry(const F& function){
// It doesn't work.
}
template <F> Entry(const std::function<F> function) :
internalFunction(function) {
}
template<F, class... Arguments>
typename FunctionType<F>::return_type operator()(Arguments... arguments){
return internalFunction(arguments...);
}
};
几件事:
template<F>
完全没有任何意义。您从 class 上的模板参数中获取 F
的类型,使用它并完全删除它。
接下来,您可能更容易在 operator()
函数中使用尾随 return 类型:
template<class... Arguments>
auto operator()(Arguments... arguments) -> decltype(internalFunction(arguments...))
{
return internalFunction(arguments...);
}
(如果你有 C++14,你可以只使用 auto
)。
这是你的固定 class
template <class F> class Entry {
std::function<F> internalFunction;
public:
Entry(const F& function){
// It doesn't work.
}
Entry(const std::function<F> function) :
internalFunction(function) {
}
template<class... Arguments>
auto operator()(Arguments... arguments) -> decltype(internalFunction(arguments...)){
return internalFunction(arguments...);
}
};
我正在编写 Monitor class 来解决同步问题,我想实现一个 'Entry' class 来包装 std::function。
我实现了一点,使用了函数特征,但现在我只能使用准备好的 std::function 对象构造 Entry 对象。尝试编写一个将普通函数作为参数的构造函数失败,并显示有关模板参数 deduction/substitution 和
程序运行正常,但我只是好奇如何实现给定的构造函数,这是我的代码:
template <class F>
struct FunctionType;
template <class R, class Object, class... Args>
struct FunctionType<R (Object::*)(Args...)> {
typedef R return_type;
};
template <class R, class Object, class... Args>
struct FunctionType<R (Object::*)(Args...) const> {
typedef R return_type;
};
template <class F> class Entry {
std::function<F> internalFunction;
...
public:
template <F> Entry(const F& function){
// It doesn't work.
}
template <F> Entry(const std::function<F> function) :
internalFunction(function) {
}
template<F, class... Arguments>
typename FunctionType<F>::return_type operator()(Arguments... arguments){
return internalFunction(arguments...);
}
};
几件事:
template<F>
完全没有任何意义。您从 class 上的模板参数中获取 F
的类型,使用它并完全删除它。
接下来,您可能更容易在 operator()
函数中使用尾随 return 类型:
template<class... Arguments>
auto operator()(Arguments... arguments) -> decltype(internalFunction(arguments...))
{
return internalFunction(arguments...);
}
(如果你有 C++14,你可以只使用 auto
)。
这是你的固定 class
template <class F> class Entry {
std::function<F> internalFunction;
public:
Entry(const F& function){
// It doesn't work.
}
Entry(const std::function<F> function) :
internalFunction(function) {
}
template<class... Arguments>
auto operator()(Arguments... arguments) -> decltype(internalFunction(arguments...)){
return internalFunction(arguments...);
}
};