【问题标题】:Instantiation of static template member function?静态模板成员函数的实例化?
【发布时间】:2018-06-04 15:25:04
【问题描述】:

我有一个问题,给定这个 sn-p 的代码:

class Thread{
    private:
    template<class t_Funtion, class ... t_Args>
    struct ThreadExiter
    {
        using CallbackType = decltype(std::bind(std::declval<t_Funtion>(), std::declval<t_Args>()...));

        static CallbackType m_Callback;

        template<class ... t_ConstructorArgs>
        ThreadExiter(t_Funtion p_Function, t_ConstructorArgs ... p_Args) :
            m_Callback(std::forward<t_Funtion>(p_Function), std::forward<t_ConstructorArgs&&>(p_Args) ...)
        {
            // Nothing to do
        }
        ~ThreadExiter()
        {
            m_Callback();
        }
    };

如何实例化静态成员static CallbackType m_Callback;

我试过了:

template<class t_Funtion, class ... t_Args> 
typename Thread::ThreadExiter<t_Funtion, t_Args...>::CallbackType Thread::ThreadExiter<t_Funtion, t_Args...>::m_Callback

但我得到了:

error: no matching function for call to 'std::_Bind<int (*(Thread*)) Thread*)>::_Bind()'  typename Thread::ThreadExiter<t_Funtion, t_Args...>::CallbackType Thread::ThreadExiter<t_Funtion, t_Args...>::m_Callback;
                                                               ^

【问题讨论】:

    标签: c++ c++11 templates stdbind


    【解决方案1】:

    尝试在ThreadExiter的构造函数中构造静态成员时出错:

    ThreadExiter(/*...*/) : m_Callback(/*...*/) {}
    

    来吧:

    class Thread
    {
    private:
        template<class t_Funtion, class ... t_Args>
        struct ThreadExiter
        {
            using CallbackType = decltype(std::bind(std::declval<t_Funtion>(), std::declval<t_Args>()...));
    
            static CallbackType m_Callback;
    
            template<class ... t_ConstructorArgs>
            ThreadExiter(t_Funtion p_Function, t_ConstructorArgs ... p_Args)
            {
                m_Callback = CallbackType{std::forward<t_Funtion>(p_Function), std::forward<t_ConstructorArgs&&>(p_Args) ...};
            }
            ~ThreadExiter()
            {
                m_Callback();
            }
        };
    };
    
    template<class t_Funtion, class ... t_Args> 
    typename Thread::ThreadExiter<t_Funtion, t_Args...>::CallbackType Thread::ThreadExiter<t_Funtion, t_Args...>::m_Callback;
    

    【讨论】:

      【解决方案2】:

      谢谢!最后我以这种方式解决了(因为我没有复制和赋值构造函数):

           static CallbackType* m_Callback;
              template<class ... t_ConstructorArgs>
              ThreadExiter(t_Funtion p_Function, t_ConstructorArgs ... p_Args)
              {
                  static CallbackType l_Callback(std::forward<t_Funtion>(p_Function), std::forward<t_ConstructorArgs&&>(p_Args) ...);
                  m_Callback = &l_Callback;
              }
      
              ~ThreadExiter()
              {
                  m_Callback->operator()();
              }
      

      以这种方式实例化:

      Template<class t_Funtion, class ... t_Args>
      using CallbackType = decltype(std::bind(std::declval<t_Funtion>(), 
      std::declval<t_Args>()...));
      
      template<class t_Funtion, class ... t_Args>
      CallbackType<t_Funtion, t_Args...>* Thread::ThreadExiter<t_Funtion, t_Args...>::m_Callback;
      

      【讨论】:

        猜你喜欢
        • 2011-04-11
        • 2017-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多