[英]Cannot overload constructor in C++
我正在制作一個僅限於std::function
的move。 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;
};
當我使用它時,我得到一個錯誤,構造函數不能相互重載。 我究竟做錯了什么? 完整代碼位於此處 。
編輯:從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]
它花了一點時間,但我得到了這個
答案完整性的片段。
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),則會出現編譯錯誤。
老實說,如果有人有更正,我會因為工作而有點眼睛,我很樂意聽到它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.