TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
C++ Coding Standard

Rule:  STY#020Checked automatically with code checker

Synopsis:Use std::function instead of function pointers
Severity Level:10


A reason to use std::function is that it offers more flexibility to the user (caller of the function): std::function accepts member- and free functions and accepts functions with bound extra arguments.

Wrong example:

int foo(int x) { return x; }
int (*foo_func)(int) = &foo;

Right example:

int foo(int x) { return x; }
std::function<int(int)> foo_func = &foo;

more elegant:

std::function<int(int)> foo_func = [](int x) { return x; };

even more elegant:

auto foo_func = [](int x) { return x; };