0

it is kinda late where I am now & I dont have much experience with threading so I may be overlooking something obvious... But I made a small example of what my error is : Source Code :

#include <thread>

struct B {
  /* Stuff */
};

struct A {

std::thread td;

A(const B& b) { td = std::thread(&A::f, b); }

void f(const B& b) { /* DO SOMETHING WTIH b */ }

};

int main() {

  B b;

  A a(b);

}

Compiling with g++ -c smallex.cpp and I dont really understand what my issue is here. I think it is pretty straight forward what I am trying to accomplish here..

In file included from smallex.cpp:1:0:
/usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (A::*)(const B&), B> >’:
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (A::*)(const B&); _Args = {const B&}]’
smallex.cpp:11:42:   required from here
/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (A::*)(const B&), B> >::_M_invoke(std::thread::_Invoker<std::tuple<void (A::*)(const B&), B> >::_Indices)’
  operator()()
  ^~~~~~~~
/usr/include/c++/7/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::*)(const B&), B>]
    _M_invoke(_Index_tuple<_Ind...>)
    ^~~~~~~~~
/usr/include/c++/7/thread:231:4: note:   template argument deduction/substitution failed:
/usr/include/c++/7/thread: In substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (A::*)(const B&), B> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1}]’:
/usr/include/c++/7/thread:240:2:   required from ‘struct std::thread::_Invoker<std::tuple<void (A::*)(const B&), B> >’
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (A::*)(const B&); _Args = {const B&}]’
smallex.cpp:11:42:   required from here
/usr/include/c++/7/thread:233:29: error: no matching function for call to ‘__invoke(std::__tuple_element_t<0, std::tuple<void (A::*)(const B&), B> >, std::__tuple_element_t<1, std::tuple<void (A::*)(const B&), B> >)’
    -> decltype(std::__invoke(_S_declval<_Ind>()...))
                ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/tuple:41:0,
                 from /usr/include/c++/7/bits/unique_ptr.h:37,
                 from /usr/include/c++/7/memory:80,
                 from /usr/include/c++/7/thread:39,
                 from smallex.cpp:1:
/usr/include/c++/7/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)
     ^~~~~~~~
/usr/include/c++/7/bits/invoke.h:89:5: note:   template argument deduction/substitution failed:
/usr/include/c++/7/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::*)(const B&); _Args = {B}]’:
/usr/include/c++/7/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::*)(const B&), B> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1}]’
/usr/include/c++/7/thread:240:2:   required from ‘struct std::thread::_Invoker<std::tuple<void (A::*)(const B&), B> >’
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (A::*)(const B&); _Args = {const B&}]’
smallex.cpp:11:42:   required from here
/usr/include/c++/7/bits/invoke.h:89:5: error: no type named ‘type’ in ‘struct std::__invoke_result<void (A::*)(const B&), B>’

Can someone please help point me in the right direction?

Notes : g++ --version

g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Brandon
  • 37
  • 4
  • 2
    TL;DR of the dupe: `td = std::thread(&A::f, b);` needs to be `td = std::thread(&A::f, this, std::cref(b));` because all member functions have an implicit first parameter of the class type and `cref` is needed to pass the reference through the thread. – NathanOliver Jan 06 '20 at 14:19
  • thank you for the quick reply, and TLDR that worked great. sorry for the repeat question – Brandon Jan 06 '20 at 14:24
  • No worries. Glad it helped. – NathanOliver Jan 06 '20 at 14:25

0 Answers0