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

  • Mastering C++ Programming
  • Jeganathan Swaminathan
  • 156字
  • 2021-07-02 18:28:46

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.

主站蜘蛛池模板: 镇巴县| 富裕县| 太原市| 玉环县| 烟台市| 九台市| 策勒县| 剑川县| 胶州市| 澄城县| 汪清县| 拉萨市| 顺昌县| 丹巴县| 安新县| 仙游县| 正宁县| 惠东县| 礼泉县| 宁明县| 即墨市| 治县。| 太保市| 界首市| 夏津县| 浦北县| 西林县| 普格县| 平乐县| 定日县| 沧源| 托克逊县| 临澧县| 萨嘎县| 永昌县| 特克斯县| 玛多县| 双流县| 鲁甸县| 河池市| 扶余县|