_STD invoke(_STD move(_STD get<_Indices>(_Tup))...);
时间: 2023-06-18 18:02:14 浏览: 208
这是一个 C++11 中的代码片段,用于调用一个函数或者一个可调用对象。其中 `_Tup` 是一个 `std::tuple` 类型的变量, `_Indices` 是一个模板参数包,代表了函数参数的下标。
`_STD move` 是将参数转化为右值引用的函数,可以提高函数调用的效率。
`_STD get<_Indices>(_Tup)` 表示从 `_Tup` 中取出第 `_Indices` 个元素。
最终这段代码会将 `_Tup` 中的各个元素作为参数传递给一个函数或者可调用对象,并执行之。
相关问题
std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...);
### 关于 `std::__invoke` 函数调用
#### 使用 `std::__invoke`
`std::__invoke` 是 C++17 中引入的一个工具函数,用于统一处理可调用对象的调用方式。无论传入的是普通函数、成员函数指针还是带有重载括号运算符的对象(仿函数),都可以通过此接口进行调用。
对于带参数的情况,如果这些参数是右值,则可以考虑配合 `std::move` 来提高性能;当涉及到元组或其他容器类型的解包操作时,通常会结合 `std::get` 和模板展开技术一起使用[^1]。
```cpp
#include <utility>
#include <tuple>
// 假设有一个类 A 的实例 a 及其成员函数 func 成员变量 b
struct A {
int func(int x) const;
};
A a;
auto&& result = std::__invoke(&A::func, &a, 42);
// 或者移动语义下的情况
int value = 100;
auto&& moved_result = std::__invoke([](int&& val){return val * 2;}, std::move(value));
// 对 tuple 进行解包并作为参数传递给目标函数
template<typename Func, typename Tuple, size_t... Is>
decltype(auto) invoke_with_tuple_impl(Func&& f, Tuple&& t, std::index_sequence<Is...>) {
return std::__invoke(std::forward<Func>(f), std::get<Is>(std::forward<Tuple>(t))...);
}
template<typename Func, typename Tuple>
decltype(auto) invoke_with_tuple(Func&& f, Tuple&& t) {
constexpr auto N = std::tuple_size_v<std::decay_t<Tuple>>;
return invoke_with_tuple_impl(
std::forward<Func>(f),
std::forward<Tuple>(t),
std::make_index_sequence<N>{}
);
}
void example() {
auto tup = std::make_tuple(1, "hello", true);
// 解包 tuple 并将其元素作为参数传递给 printf
invoke_with_tuple(+[](const char* str, bool flag, int num){
printf("%s %d\n", str, static_cast<int>(flag));
}, tup);
}
```
In file included from /home/yhdr/2-test-2023-06_v3/sent.h:24:0, from /home/yhdr/2-test-2023-06_v3/sent.cpp:1: /usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(double*, double&, double&, double&, double&, double&), double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double> > >’: /usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(double*, double&, double&, double&, double&, double&); _Args = {double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>}]’ /home/yhdr/2-test-2023-06_v3/sent.cpp:18:153: required from here /usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (*)(double*, double&, double&, double&, double&, double&), double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double> > >::_M_invoke(std::thread::_Invoker<std::tuple<void (*)(double*, double&, double&, double&, double&, double&), double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double> > >::_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 (*)(double*, double&, double&, double&, double&, double&), double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double> >] _M_invoke(_Index_tuple<_Ind...>)
这个编译错误看起来是在创建一个std::thread对象时出现了问题。它指出无法匹配std::thread::_Invoker模板的_M_invoke函数的参数列表。这通常是因为您的函数参数与std::thread的参数不匹配,或者您的函数参数中有无法转换的类型。
请检查您的函数参数类型是否与std::thread需要的参数类型相匹配。如果您的函数需要接受其他参数类型,请尝试将它们转换为std::reference_wrapper或其他可转换的类型。您可能还需要检查您的函数参数数量是否正确。
如果您需要更多的帮助,请提供更多的代码和上下文,我可以更好地帮助您解决这个问题。
阅读全文
相关推荐

















