0

I'm 'modernizing' some piece of code to c++11 'standards' (std::thread etc), I'm trying to spawn a thread using std::thread and passing a class as a parameter but I can't get it to work for some reason..

Class definition:

Class Listener
{
...
private:
...
    Class Worker
    {
    ...
    public:
    Worker(struct sockaddr_in server_sockaddr, struct sockaddr_in client_sockaddr, socklen_t blah, int blah2)
    { 
        ....
    }
    };
...
int mah_function_in_class_Listener()
{
    ...
    thread thread_id=thread(Worker(server_sockaddr, client_sockaddr, client_addr_size, accpt));
    ...
}
};

When I try to compile this I get:

In file included from /usr/include/c++/4.8/thread:39:0,
                 from ./tcp_server_singlethreaded.cpp:26:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<Listener::Worker()>’:
/usr/include/c++/4.8/thread:137:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = Listener::Worker; _Args = {}]’
./tcp_server_singlethreaded.cpp:199:106:   required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<Listener::Worker()>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<Listener::Worker()>’
         _M_invoke(_Index_tuple<_Indices...>)
         ^

As far as I understand this is happening because an std::thread object is being returned, rather than object thread_id of type std::thread (?? :S), I thought a copy initialization or using {} would resolve this, but apparently there's something else wrong here..

Thanks for taking a look - hopefully someone will be able to help with this

Many thanks!

EDIT

I forgot to mention a minor detail.. g++ / gcc 4.8.2 (4.8.2-19 ubuntu) -- I don't think this is a gcc prob (more likely a pebkac issue)..

jww
  • 97,681
  • 90
  • 411
  • 885
  • Not entirely sure what you want but maybe [this](http://stackoverflow.com/a/10673671/893693) can help you. – Stephan Dollberg Feb 15 '15 at 22:13
  • `Worker` doesn't have an `operator()` - what are you expecting the new thread to do with it? – Casey Feb 15 '15 at 22:13
  • Hi Casey and bamboon - thanks for trying to help! bamboon: close, but not the same i guess, thanks though! Casey: I omitted stuff from both classes for brevity, don't think what's in there will matter as I'm getting a compiler error about the type of the object returned? – user3456251 Feb 15 '15 at 22:23
  • Would be nice to show us your `operator()`, because I managed to compile this snippet, when I added a `public: void operator() ()` without any arguments. *(by the way, I suppose your `class` definitions are lower case in real code)* – Christophe Feb 15 '15 at 23:05

0 Answers0