cpp17的 std::invoke
时间: 2023-05-31 17:03:45 浏览: 72
std::invoke是C++17标准库中的一个新函数,用于调用函数或者函数对象。它可以方便地处理函数指针、成员函数指针和函数对象的调用,同时支持传递参数和返回值。
这个函数的语法如下:
template <typename F, typename... Args>
decltype(auto) invoke(F&& f, Args&&... args);
其中,F表示要调用的函数或函数对象,Args表示调用该函数时要传递的参数。
使用std::invoke的好处在于,它可以在不同的调用方式下,提供一致的语法和错误处理,使代码更加清晰易懂。例如:
// 调用普通函数
int add(int a, int b) { return a + b; }
int result1 = std::invoke(add, 1, 2);
// 调用成员函数
struct Foo {
int add(int a, int b) { return a + b; }
};
Foo foo;
int result2 = std::invoke(&Foo::add, foo, 1, 2);
// 调用函数对象
struct Bar {
int operator()(int a, int b) { return a + b; }
};
Bar bar;
int result3 = std::invoke(bar, 1, 2);
在上面的例子中,我们使用了std::invoke来调用普通函数、成员函数和函数对象,都使用了相同的语法,使代码更加简洁易懂。
相关问题
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或其他可转换的类型。您可能还需要检查您的函数参数数量是否正确。
如果您需要更多的帮助,请提供更多的代码和上下文,我可以更好地帮助您解决这个问题。
C#调用c++std::function
C#可以通过使用Platform Invoke(P/Invoke)来调用C++的std::function。P/Invoke是一种机制,允许C#代码调用非托管的C/C++代码。下面是一个示例,演示了如何在C#中调用C++的std::function。
首先,需要在C#中声明C++的std::function。可以使用`[DllImport]`特性来声明C++函数。假设C++的std::function定义如下:
```cpp
#include <functional>
typedef std::function<int(int, int)> MathFunction;
extern "C" __declspec(dllexport) MathFunction GetAddFunction();
```
然后,在C#中声明C++的std::function:
```csharp
using System;
using System.Runtime.InteropServices;
public delegate int MathFunction(int a, int b);
public static class NativeMethods
{
[DllImport("YourCppLibrary.dll")]
public static extern MathFunction GetAddFunction();
}
```
接下来,可以在C#中调用C++的std::function:
```csharp
MathFunction addFunction = NativeMethods.GetAddFunction();
int result = addFunction(2, 3);
Console.WriteLine(result); // 输出:5
```
在上面的示例中,我们首先通过`NativeMethods.GetAddFunction()`获取C++的std::function对象,然后将其赋值给C#的`MathFunction`委托。最后,我们可以像调用普通的C#委托一样调用`addFunction`来执行C++的函数。