Почему std::condition_variable как член класса вызывает ошибки компиляции с std::thread?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Почему std::condition_variable как член класса вызывает ошибки компиляции с std::thread?

Сообщение Anonymous »

Я пытался включить std::condition_variable в качестве члена класса и получил множество ошибок компиляции при передаче объекта этого класса в std::thread. Я сократил весь остальной код из своей реальной программы и получил приведенный ниже минимальный код. Удаление std::condition_variable не вызывает проблем. Я пробовал «инициализировать» переменную в конструкторе, а также сделать ее встроенной, но ничего не помогло.
#include
#include

struct ThreadHandler {
void operator()() { }

std::condition_variable cond;
};

int main() {
ThreadHandler th1;
std::thread t1(th1);
t1.join();
}

Что я здесь делаю не так?
Ниже я получаю ошибку компиляции:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread: In instantiation of ‘static std::thread::_Invoker std::thread::__make_invoker(_Callable&&, _Args&& ...) [with _Callable = ThreadHandler&; _Args = {}; typename std::decay::type = ThreadHandler]’:
/usr/local/include/c++/8.1.0/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = ThreadHandler&; _Args = {}]’
main.cpp:14:23: required from here
/usr/local/include/c++/8.1.0/thread:258:4: error: no matching function for call to ‘std::tuple::tuple(
)’
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:828:11: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, std::tuple&&)’
explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
^~~~~
/usr/local/include/c++/8.1.0/tuple:828:11: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 3 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:813:2: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, std::tuple&&)’
tuple(allocator_arg_t __tag, const _Alloc& __a,
^~~~~
/usr/local/include/c++/8.1.0/tuple:813:2: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 3 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:798:11: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple&)’
explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
^~~~~
/usr/local/include/c++/8.1.0/tuple:798:11: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 3 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:783:2: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple&)’
tuple(allocator_arg_t __tag, const _Alloc& __a,
^~~~~
/usr/local/include/c++/8.1.0/tuple:783:2: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 3 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:771:2: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, std::tuple&&)’
tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
^~~~~
/usr/local/include/c++/8.1.0/tuple:771:2: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 3 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:767:2: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple&)’
tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
^~~~~
/usr/local/include/c++/8.1.0/tuple:767:2: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 3 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:761:11: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, _UElements&& ...)’
explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
^~~~~
/usr/local/include/c++/8.1.0/tuple:761:11: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects at least 2 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:750:2: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, _UElements&& ...)’
tuple(allocator_arg_t __tag, const _Alloc& __a,
^~~~~
/usr/local/include/c++/8.1.0/tuple:750:2: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects at least 2 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:740:11: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, const _Elements& ...)’
explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
^~~~~
/usr/local/include/c++/8.1.0/tuple:740:11: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 3 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:729:2: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&, const _Elements& ...)’
tuple(allocator_arg_t __tag, const _Alloc& __a,
^~~~~
/usr/local/include/c++/8.1.0/tuple:729:2: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 3 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:719:2: note: candidate: ‘template std::tuple::tuple(std::allocator_arg_t, const _Alloc&)’
tuple(allocator_arg_t __tag, const _Alloc& __a)
^~~~~
/usr/local/include/c++/8.1.0/tuple:719:2: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 2 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:713:28: note: candidate: ‘template constexpr std::tuple::tuple(std::tuple&&)’
explicit constexpr tuple(tuple&& __in)
^~~~~
/usr/local/include/c++/8.1.0/tuple:713:28: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: ‘ThreadHandler’ is not derived from ‘std::tuple’
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:702:19: note: candidate: ‘template constexpr std::tuple::tuple(std::tuple&&)’
constexpr tuple(tuple&& __in)
^~~~~
/usr/local/include/c++/8.1.0/tuple:702:19: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: ‘ThreadHandler’ is not derived from ‘std::tuple’
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:690:28: note: candidate: ‘template constexpr std::tuple::tuple(const std::tuple&)’
explicit constexpr tuple(const tuple& __in)
^~~~~
/usr/local/include/c++/8.1.0/tuple:690:28: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: ‘ThreadHandler’ is not derived from ‘const std::tuple’
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:678:19: note: candidate: ‘template constexpr std::tuple::tuple(const std::tuple&)’
constexpr tuple(const tuple& __in)
^~~~~
/usr/local/include/c++/8.1.0/tuple:678:19: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: ‘ThreadHandler’ is not derived from ‘const std::tuple’
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:662:17: note: candidate: ‘constexpr std::tuple::tuple(std::tuple&&) [with _Elements = {ThreadHandler}]’
constexpr tuple(tuple&&) = default;
^~~~~
/usr/local/include/c++/8.1.0/tuple:662:17: note: no known conversion for argument 1 from ‘ThreadHandler’ to ‘std::tuple&&’
/usr/local/include/c++/8.1.0/tuple:657:28: note: candidate: ‘template::type > constexpr std::tuple::tuple(_UElements&& ...)’
explicit constexpr tuple(_UElements&&... __elements)
^~~~~
/usr/local/include/c++/8.1.0/tuple:657:28: note: template argument deduction/substitution failed:
/usr/local/include/c++/8.1.0/tuple:656:21: error: no type named ‘type’ in ‘struct std::enable_if’
bool>::type=false>
^~~~~
/usr/local/include/c++/8.1.0/tuple:646:19: note: candidate: ‘template::type > constexpr std::tuple::tuple(_UElements&& ...)’
constexpr tuple(_UElements&&... __elements)
^~~~~
/usr/local/include/c++/8.1.0/tuple:646:19: note: template argument deduction/substitution failed:
/usr/local/include/c++/8.1.0/tuple:645:21: error: no type named ‘type’ in ‘struct std::enable_if’
bool>::type=true>
^~~~
/usr/local/include/c++/8.1.0/tuple:619:26: note: candidate: ‘template::type > constexpr std::tuple::tuple(const _Elements& ...)’
explicit constexpr tuple(const _Elements&... __elements)
^~~~~
/usr/local/include/c++/8.1.0/tuple:619:26: note: template argument deduction/substitution failed:
/usr/local/include/c++/8.1.0/tuple:618:28: error: no type named ‘type’ in ‘struct std::enable_if’
bool>::type=false>
^~~~~
/usr/local/include/c++/8.1.0/tuple:608:19: note: candidate: ‘template::type > constexpr std::tuple::tuple(const _Elements& ...)’
constexpr tuple(const _Elements&... __elements)
^~~~~
/usr/local/include/c++/8.1.0/tuple:608:19: note: template argument deduction/substitution failed:
/usr/local/include/c++/8.1.0/tuple:607:28: error: no type named ‘type’ in ‘struct std::enable_if’
bool>::type=true>
^~~~
/usr/local/include/c++/8.1.0/tuple:591:26: note: candidate: ‘template constexpr std::tuple::tuple()’
explicit constexpr tuple()
^~~~~
/usr/local/include/c++/8.1.0/tuple:591:26: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 0 arguments, 1 provided
} };
^
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple:581:17: note: candidate: ‘template constexpr std::tuple::tuple()’
constexpr tuple()
^~~~~
/usr/local/include/c++/8.1.0/tuple:581:17: note: template argument deduction/substitution failed:
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread:258:4: note: candidate expects 0 arguments, 1 provided
} };
^
/usr/local/include/c++/8.1.0/thread:258:4: error: could not convert ‘{}’ from ‘
’ to ‘std::thread::_Invoker’
In file included from /usr/local/include/c++/8.1.0/bits/unique_ptr.h:37,
from /usr/local/include/c++/8.1.0/memory:80,
from /usr/local/include/c++/8.1.0/thread:39,
from main.cpp:1:
/usr/local/include/c++/8.1.0/tuple: In instantiation of ‘constexpr std::_Head_base::_Head_base(_UHead&&) [with _UHead = ThreadHandler; long unsigned int _Idx = 0; _Head = ThreadHandler]’:
/usr/local/include/c++/8.1.0/tuple:373:49: required from ‘constexpr std::_Tuple_impl::_Tuple_impl(std::_Tuple_impl&&) [with long unsigned int _Idx = 0; _Head = ThreadHandler]’
/usr/local/include/c++/8.1.0/tuple:662:17: required from ‘std::thread::_State_impl::_State_impl(_Callable&&) [with _Callable = std::thread::_Invoker]’
/usr/local/include/c++/8.1.0/thread:197:20: required from ‘static std::thread::_State_ptr std::thread::_S_make_state(_Callable&&) [with _Callable = std::thread::_Invoker; std::thread::_State_ptr = std::unique_ptr]’
/usr/local/include/c++/8.1.0/thread:126:38: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = ThreadHandler&; _Args = {}]’
main.cpp:14:23: required from here
/usr/local/include/c++/8.1.0/tuple:133:42: error: use of deleted function ‘ThreadHandler::ThreadHandler(ThreadHandler&&)’
: _M_head_impl(std::forward(__h)) { }
^
main.cpp:4:8: note: ‘ThreadHandler::ThreadHandler(ThreadHandler&&)’ is implicitly deleted because the default definition would be ill-formed:
struct ThreadHandler
^~~~~~~~~~~~~
main.cpp:4:8: error: use of deleted function ‘std::condition_variable::condition_variable(const std::condition_variable&)’
In file included from main.cpp:2:
/usr/local/include/c++/8.1.0/condition_variable:82:5: note: declared here
condition_variable(const condition_variable&) = delete;
^~~~~~~~~~~~~~~~~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/thread: In instantiation of ‘std::thread::_State_impl::_State_impl(_Callable&&) [with _Callable = std::thread::_Invoker]’:
/usr/local/include/c++/8.1.0/thread:197:20: required from ‘static std::thread::_State_ptr std::thread::_S_make_state(_Callable&&) [with _Callable = std::thread::_Invoker; std::thread::_State_ptr = std::unique_ptr]’
/usr/local/include/c++/8.1.0/thread:126:38: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = ThreadHandler&; _Args = {}]’
main.cpp:14:23: required from here
/usr/local/include/c++/8.1.0/thread:221:14: note: synthesized method ‘constexpr std::tuple::tuple(std::tuple&&) [with _Elements = {ThreadHandler}]’ first required here
struct _Invoker
^~~~~~~~
/usr/local/include/c++/8.1.0/thread:182:69: note: synthesized method ‘constexpr std::thread::_Invoker::_Invoker(std::thread::_Invoker&&)’ first required here
_State_impl(_Callable&& __f) : _M_func(std::forward(__f))


Подробнее здесь: https://stackoverflow.com/questions/510 ... s-with-std
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»