error: no type named ‘type’ in ‘class std::result_of
c++, multithreading
Solution
It seems you've encountered the most vexing parse
TTAS t();
`t` is a function taking no arguments and returning `TTAS`.
Prefer uniform initialization1 syntax over the legacy one:
TTAS t{}; // no ambiguity here
There's one more problem with your code `test2` takes its argument by value, but `TTAS` contains `std::atomic<bool>` which is neither copyable nor movable.
1 although this is somewhat a misnomer, as it's not all that uniform
Problem
I know this type of question has already been asked, but I'm not able figure out the reason for the errors I'm getting.. I'm trying to test a lock implementation as given below using multiple threads: ``` class TTAS { atomic<bool> state; public: TTAS(){ state = ATOMIC_FLAG_INIT; } void lock() { while(true) { while(state) {}; // using exchange() which is equivalent to getAndSet but with lookup if (!state.exchange(true)) { return; } } } void unlock() { state.exchange(false); } }; ``` I'm creating threads for this using the below code: ``` void test2(TTAS t) { } TTAS t(); for (int i = 0; i < 10; i++) { // Create a thread and push it into the thread list and call the distributedWrite function threadList.push_back(std::thread(test2, std::ref(t))); } ``` When I compile this code I get the below error. I don't know what I'm doing wrong here.. ``` In file included from /usr/include/c++/5/thread:39:0, from assgn4.cpp:17: /usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’: /usr/include/c++/5/thread:137:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(TTAS); _Args = {std::reference_wrapper<TTAS()>}]’ assgn4.cpp:186:60: required from here /usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’ typedef typename result_of<_Callable(_Args...)>::type result_type; ^ /usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’ _M_invoke(_Index_tuple<_Indices...>) ^ ``` Could someone please explain this error. Thanks in advance