我正在制作仅限移动std::function
。 move_function
包含指向基类的指针,move_function_base
类型会删除底层函子类型。 move_function_imp
继承自move_function_base
并保存类型化的底层函子。 move_function_imp
的定义如下:
template<class F, class ReturnType, class... ParamTypes>
class move_function_imp : public move_function_base<ReturnType, ParamTypes...> {
typename std::remove_reference<F>::type f_;
public:
virtual ReturnType callFunc(ParamTypes&&... p) override {
return f_(std::forward<ParamTypes>(p)...);
}
explicit move_function_imp(const F& f) : f_(f) {}
explicit move_function_imp(F&& f) : f_(std::move(f)) {}
move_function_imp() = delete;
move_function_imp(const move_function_imp&) = delete;
move_function_imp& operator=(const move_function_imp&) = delete;
};
当我使用它时,我得到一个错误,构造函数不能相互重载。我究竟做错了什么?完整代码位于here。
编辑:从ideone链接粘贴的错误:
prog.cpp: In instantiation of ‘class move_function_imp<main()::__lambda0&, void>’:
prog.cpp:39:30: required from ‘move_function<ReturnType(ParamTypes ...)>::move_function(F&&) [with F = main()::__lambda0&; ReturnType = void; ParamTypes = {}]’
prog.cpp:62:38: required from here
prog.cpp:20:12: error: ‘move_function_imp<F, ReturnType, ParamTypes>::move_function_imp(F&&) [with F = main()::__lambda0&; ReturnType = void; ParamTypes = {}]’ cannot be overloaded
explicit move_function_imp(F&& f) : f_(std::move(f)) {}
^
prog.cpp:19:12: error: with ‘move_function_imp<F, ReturnType, ParamTypes>::move_function_imp(const F&) [with F = main()::__lambda0&; ReturnType = void; ParamTypes = {}]’
explicit move_function_imp(const F& f) : f_(f) {}
^
prog.cpp:19:12: error: ‘move_function_imp<F, ReturnType, ParamTypes>::move_function_imp(const F&) [with F = main()::__lambda0&; ReturnType = void; ParamTypes = {}]’, declared using local type ‘main()::__lambda0’, is used but never defined [-fpermissive]
答案 0 :(得分:1)
我花了一点时间,但我得到了this
回答完整性的摘录。
template <class>
struct remove_reference_except_function {};
template <class R, class... Args>
struct remove_reference_except_function<R(&)(Args...)>
{
typedef R(&type)(Args...);
};
template <class R, class... Args>
struct remove_reference_except_function<R(&)(Args......)> //varardic function
{
typedef R(&type)(Args......);
};
//I dont think you can have an rvalue reference to a function? Or can you dereference a non-capturing lambda?
template <class T>
struct remove_reference_except_function<T &>
{
typedef T type;
};
template <class T>
struct remove_reference_except_function<T &&>
{
typedef T type;
};
template< class ReturnType, class... ParamTypes>
struct move_function_base{
virtual ReturnType callFunc(ParamTypes... p) = 0;
};
template<class F, class ReturnType, class... ParamTypes>
class move_function_imp : public move_function_base<ReturnType, ParamTypes...> {
//Using std::remove_reference on a normal function gives you an invalid type for declaring a variable. Hence the custom reference removal
typename remove_reference_except_function<F>::type f_;
public:
virtual ReturnType callFunc(ParamTypes... p) override {
return f_(std::forward<ParamTypes>(p)...);
}
explicit move_function_imp(const typename std::remove_reference<F>::type& f) : f_(f) {}
explicit move_function_imp(typename std::remove_reference<F>::type&& f) : f_(std::move(f)) {}
move_function_imp() = delete;
move_function_imp(const move_function_imp&) = delete;
move_function_imp& operator=(const move_function_imp&) = delete;
};
正如人们指出你的主要问题是模板参数折叠到同一类型会给你带来重载错误,所以我用普通的std :: remove_reference来解决这个问题。
你也在move_function_imp的callFunc中有一个流氓右值引用。
我必须创建一个自定义remove_reference来声明f_,因为如果从正常创建的函数中移除了引用(在我的示例中为ff),则会出现编译错误。
老实说,如果有人有更正,我会因为工作而感到有点瞎眼,我很乐意听到它们。