Search code examples
c++c++11templatesstdbind

Instantiation of static template member function?


I have a problem, given this snippet of code:

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();
        }
    };

How can I instantiate the static member static CallbackType m_Callback; ?

I tried:

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

But I got:

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;
                                                               ^

Solution

  • Thank you! In the end I resolved in this way (cause I didn't have copy and assignment constructor):

         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()();
            }
    

    Instantiated this way:

    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;