官术网_书友最值得收藏!

The std::invoke( ) method

The std::invoke() method can be used to call functions, function pointers, and member pointers with the same syntax:

#include <iostream>
#include <functional>
using namespace std;

void globalFunction( ) {
cout << "globalFunction ..." << endl;
}

class MyClass {
public:
void memberFunction ( int data ) {
std::cout << "\nMyClass memberFunction ..." << std::endl;
}

static void staticFunction ( int data ) {
std::cout << "MyClass staticFunction ..." << std::endl;
}
};

int main ( ) {

MyClass obj;

std::invoke ( &MyClass::memberFunction, obj, 100 );
std::invoke ( &MyClass::staticFunction, 200 );
std::invoke ( globalFunction );

return 0;
}

The preceding code can be compiled and the output can be viewed with the following commands:

g++-7 main.cpp -std=c++17
./a.out

The output of the preceding program is as follows:

MyClass memberFunction ...
MyClass staticFunction ...
globalFunction ...

The std::invoke( ) method is a template function that helps you seamlessly invoke callable objects, both built-in and user-defined.

主站蜘蛛池模板: 桂东县| 科尔| 新民市| 永丰县| 分宜县| 科尔| 新丰县| 田林县| 修水县| 收藏| 湘潭市| 鹤壁市| 涞水县| 仁化县| 山阴县| 新丰县| 温宿县| 邓州市| 黎平县| 大埔县| 贵德县| 巨野县| 石嘴山市| 阳东县| 富源县| 辽阳市| 揭阳市| 陕西省| 杭锦旗| 织金县| 临夏市| 闵行区| 竹山县| 平昌县| 韩城市| 沙坪坝区| 河池市| 诸暨市| 久治县| 上杭县| 津南区|