1

次の完全なテスト ケースでは、最初の 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.
4

2 に答える 2