在下面的完整测试用例中,如果我使用第一个 ctor 按值获取函子并将其移动到位,那么代码将按预期编译并工作。
但是,如果我使用通用引用 ctor,它将无法编译(我在下面包含了 clang 错误消息)。
我该如何解决这个问题或我做错了什么?
#include <functional>
#include <utility>
#include <exception>
template<typename F>
struct py_catch {
F func;
/*
//Works
py_catch(F f)
: func ( std::move(f) )
{ } */
//Doesn't
template<typename F2>
py_catch(F2&& f)
: func ( std::forward<F2>(f) )
{ }
py_catch(py_catch&&)=default;
py_catch(const py_catch&)=default;
py_catch& operator=(const py_catch&)=default;
py_catch& operator=(py_catch&&)=default;
template<typename... Args>
auto operator()(Args&&... args)
-> decltype(func(std::forward<Args>(args)...)) {
try {
return func(std::forward<Args>(args)...);
}
catch(const std::exception&) {
throw;
}
}
};
template<typename F>
py_catch<typename std::remove_reference<F>::type> make_py_catch(F&& f) {
return py_catch<typename std::remove_reference<F>::type>(std::forward<F>(f));
}
int main() {
std::function<void()> s;
s = make_py_catch([]{});
}
编译错误:
testcase2.cpp:16:7: error: no matching constructor for initialization of '<lambda at testcase2.cpp:43:23>'
: func ( std::forward<F2>(f) )
^ ~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/functional:1764:10: note: in instantiation of function template specialization 'py_catch<<lambda at testcase2.cpp:43:23> >::py_catch<py_catch<<lambda at testcase2.cpp:43:23> > &>' requested here
new _Functor(*__source._M_access<_Functor*>());
^
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/functional:1799:8: note: in instantiation of member function 'std::_Function_base::_Base_manager<py_catch<<lambda at testcase2.cpp:43:23> > >::_M_clone' requested here
_M_clone(__dest, __source, _Local_storage());
^
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/functional:2298:33: note: in instantiation of member function 'std::_Function_base::_Base_manager<py_catch<<lambda at testcase2.cpp:43:23> > >::_M_manager' requested here
_M_manager = &_My_handler::_M_manager;
^
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/functional:2173:4: note: in instantiation of function template specialization 'std::function<void ()>::function<py_catch<<lambda at testcase2.cpp:43:23> > >' requested here
function(std::forward<_Functor>(__f)).swap(*this);
^
testcase2.cpp:43:7: note: in instantiation of function template specialization 'std::function<void ()>::operator=<py_catch<<lambda at testcase2.cpp:43:23> > >' requested here
s = make_py_catch([]{});
^
testcase2.cpp:43:23: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'py_catch<<lambda at testcase2.cpp:43:23> >' to 'const <lambda at testcase2.cpp:43:23>' for 1st argument
s = make_py_catch([]{});
^
testcase2.cpp:43:23: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'py_catch<<lambda at testcase2.cpp:43:23> >' to '<lambda at testcase2.cpp:43:23>' for 1st argument
s = make_py_catch([]{});
^
testcase2.cpp:43:23: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
1 error generated.