jeudi 16 juillet 2020

trying to use boost::bind with members function

I'm trying to use boost::bind and boost::asio from std::thread that created in member function that calls different member function (I hope that I make sense)

this is my code example:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <thread>

template<typename T>
class A
{
    public:
    void run(const boost::system::error_code& /*e*/,
    boost::asio::deadline_timer* th,bool& flag)
    {
        if(flag)
        {
            //do somthing;
             th->expires_at(th->expires_at() + boost::posix_time::minutes(1));
             th->async_wait(boost::bind(&A< T>::run,this,boost::asio::placeholders::error, th, flag));
        }
    }
    
    void initBoost(bool& flag)
    {
        boost::asio::io_service io;
        boost::asio::deadline_timer th(io, boost::posix_time::seconds(1));
        th.async_wait(boost::bind(&A<T>::run,this,boost::asio::placeholders::error, &th, flag));
        io.run();
    }
    
    void init()
    {
        bool flag;
        std::thread t(&A<T>::initBoost,this,flag);
        t.detach();
        
    }
};


int main()
{
 A<int> a;
 a.init();
 std::cout <<"end\n";
 return 0;
}

link to compiler explorer. and this is the errors that I'm getting:

In file included from <source>:4:0:
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread: In
 instantiation of 'struct std::thread::_Invoker<std::tuple<void
 (A<int>::*)(bool&), bool*> >':
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:127:22:  
 required from 'std::thread::thread(_Callable&&, _Args&& ...) [with
 _Callable = void (A<int>::*)(bool&); _Args = {bool*}]'
 
 <source>:32:21:   required from 'void A<T>::init() [with T = int]'
 
 <source>:41:9:   required from here
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:240:2:
 error: no matching function for call to
 'std::thread::_Invoker<std::tuple<void (A<int>::*)(bool&), bool*>
 >::_M_invoke(std::thread::_Invoker<std::tuple<void (A<int>::*)(bool&), bool*> >::_Indices)'
 
   operator()()
 
   ^~~~~~~~
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:231:4: note:
 candidate: template<long unsigned int ..._Ind> decltype
 (std::__invoke((_S_declval<_Ind>)()...))
 std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind ...>)
 [with long unsigned int ..._Ind = {_Ind ...}; _Tuple = std::tuple<void
 (A<int>::*)(bool&), bool*>]
 
     _M_invoke(_Index_tuple<_Ind...>)
 
     ^~~~~~~~~
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:231:4: note:
 template argument deduction/substitution failed:
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread: In
 substitution of 'template<long unsigned int ..._Ind> decltype
 (std::__invoke(_S_declval<_Ind>()...))
 std::thread::_Invoker<std::tuple<void (A<int>::*)(bool&), bool*>
 >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1}]':
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:240:2:  
 required from 'struct std::thread::_Invoker<std::tuple<void
 (A<int>::*)(bool&), bool*> >'
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:127:22:  
 required from 'std::thread::thread(_Callable&&, _Args&& ...) [with
 _Callable = void (A<int>::*)(bool&); _Args = {bool*}]'
 
 <source>:32:21:   required from 'void A<T>::init() [with T = int]'
 
 <source>:41:9:   required from here
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:233:29:
 error: no matching function for call to
 '__invoke(std::__tuple_element_t<0, std::tuple<void
 (A<int>::*)(bool&), bool*> >, std::__tuple_element_t<1,
 std::tuple<void (A<int>::*)(bool&), bool*> >)'
 
     -> decltype(std::__invoke(_S_declval<_Ind>()...))
 
                 ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
 
 In file included from
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/tuple:41:0,
                  from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/unique_ptr.h:37,
                  from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/memory:80,
                  from /celibs/boost_1_64_0/boost/asio/detail/addressof.hpp:21,
                  from /celibs/boost_1_64_0/boost/asio/detail/handler_alloc_helpers.hpp:19,
                  from /celibs/boost_1_64_0/boost/asio/detail/bind_handler.hpp:19,
                  from /celibs/boost_1_64_0/boost/asio/detail/wrapped_handler.hpp:18,
                  from /celibs/boost_1_64_0/boost/asio/io_service.hpp:24,
                  from /celibs/boost_1_64_0/boost/asio/basic_io_object.hpp:19,
                  from /celibs/boost_1_64_0/boost/asio/basic_socket.hpp:20,
                  from /celibs/boost_1_64_0/boost/asio/basic_datagram_socket.hpp:20,
                  from /celibs/boost_1_64_0/boost/asio.hpp:21,
                  from <source>:2:
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/invoke.h:89:5:
 note: candidate: template<class _Callable, class ... _Args> constexpr
 typename std::__invoke_result<_Functor, _ArgTypes>::type
 std::__invoke(_Callable&&, _Args&& ...)
      __invoke(_Callable&& __fn, _Args&&... __args)
 
      ^~~~~~~~
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/invoke.h:89:5:
 note:   template argument deduction/substitution failed:
 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/invoke.h: In
 substitution of 'template<class _Callable, class ... _Args> constexpr
 typename std::__invoke_result<_Functor, _ArgTypes>::type
 std::__invoke(_Callable&&, _Args&& ...) [with _Callable = void
 (A<int>::*)(bool&); _Args = {bool*}]': 
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:233:29:  
 required by substitution of 'template<long unsigned int ..._Ind>
 decltype (std::__invoke(_S_declval<_Ind>()...))
 std::thread::_Invoker<std::tuple<void (A<int>::*)(bool&), bool*>
 >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1}]'
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:240:2:  
 required from 'struct std::thread::_Invoker<std::tuple<void
 (A<int>::*)(bool&), bool*> >'
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/thread:127:22:  
 required from 'std::thread::thread(_Callable&&, _Args&& ...) [with
 _Callable = void (A<int>::*)(bool&); _Args = {bool*}]'
 <source>:32:21:   required from 'void A<T>::init() [with T = int]'
 <source>:41:9:   required from here
 /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/bits/invoke.h:89:5:
 error: no type named 'type' in 'struct std::__invoke_result<void
 (A<int>::*)(bool&), bool*>'

Aucun commentaire:

Enregistrer un commentaire