CodeRef
CodeRef
  • Software
    • VSCode
  • Midjourney_AI
    • Midjourney Cheat Sheet
  • aws
    • S3 Reference
    • Services
  • bash
    • .bashrc File Contents
    • CAN
    • _System Config File Locations
    • argument-length-limits
    • Conditionals
    • Data Structures
    • File Permissions
    • File Syncing
    • File System
    • Functions
    • General
    • Loops
    • My Functions
    • Networking
    • Number Operations
    • OpenVPN
    • Operators
    • Resource Management
    • Serial RS232
    • Spinning Wheel Animation
    • SSH
    • Text Operations
    • Environment Variables
  • cpp
    • ChatGPT | Pointers vs. References
    • arrays
    • Classes
    • Data Types / Structures
    • Enumerated Classes
    • Exception Handling
    • Function Objects
    • Functions
    • I/O
    • Loops
    • Macros
    • Namespaces
    • New Features
    • Pointers
    • Scope
    • Smart Pointers
    • Raw String Literals
    • Style Guide
    • Switch Case
    • Templating
    • How to Use tinyxml2
    • Useful Libraries
    • google-test
    • Conditionals
    • Rule of Three/Five
    • Optional Parameters
    • Keywords
    • Filesystem
    • Random
    • Casting
    • tools
  • git
    • Code Review Dependency Strategy
    • Git Bisect Guide
    • Git Reference
    • removing-cherry-picks
    • Useful Tools
    • Graphite Reference
  • js
    • functions
    • Javascript Reference
  • linux
    • Display
    • Dual Boot with Windows
    • File System
    • NVIDIA
    • Sending/Receiving TCP/UDP Messages in Ubuntu
    • dynamically_linked_binaries
  • markdown
    • Images
    • obsidian-reference
  • python
    • Classes
    • Exceptions
    • Functions
    • Operations
    • Python Reference
    • unittest_command-line-args
    • unittest_magicmock_GPT
    • unittest_mock
    • unittest_printing
    • unittest_subtest
    • useful-stuff
    • jupyter
    • poetry
  • ros
    • _ROS Cheat Sheet
    • Create New Workspace
    • Install ROS
    • Node Sample - Listener
    • Node Sample - Talker
    • Node Template
    • Setup
    • urdf
  • excel
    • excel-reference
  • windows
    • File System
    • WSL - Windows Subsystem for Linux
    • WSL
  • software_engineering
    • uncle_bob_lectures
      • Overview
      • Lesson 01 - Notes
  • web
    • Front End
    • Hugo
    • new_frontend_tools
  • sql
    • cheatsheet
Powered by GitBook
On this page
  1. cpp

Function Objects

Function objects are used for:

  • Storing a function to a variable

  • Creating a pointer to a function

  • Callback function - Passing a function as an argument to another function

#include <functional>
std::function

Function pointers - Google Bard example:

void modifyFunction(void (*funcPtr)(int)) {
	// Modify the function pointed to by funcPtr
	funcPtr = &anotherFunction;
}

void originalFunction(int x) {
	std::cout << "Original function: x = " << x << std::endl;
}

void anotherFunction(int x) {
	std::cout << "Modified function: x = " << x << std::endl;
}

int main() {
	// Declare a function pointer
	void (*funcPtr)(int) = &originalFunction;

	// Call the modifyFunction function
	modifyFunction(funcPtr);

	// Call the function pointer
	funcPtr(5);
	
	return 0;
}

Function pointers

void originalFunction(int x) {
	std::cout << "Original function: x = " << x << std::endl;
}

// Declare a function pointer, and assign the
// existing function to it.

void (*funcPtr)(int); // Assign later
funcPtr = &originalFunction;

void (*funcPtr)(int) = &originalFunction; // Assign now.

// Multiple arguments
void (*funcPtr)(int, std::string);

// Call a function by its function pointer
(*funcPtr)(42);
funcPtr(42); // This also works, but less clear what you're doing.

// THE BELOW MIGHT ALSO WORK, NOT SURE.

// Passing by function pointer, defined beforehand.
typedef return_type (*func_ptr)(arg1_type, arg2_type);
void my_func (func_ptr func_name) {}

// Passing by reference
void my_func (return_type (&func_name)(arg1_type, arg2_type)) {}

Creating a variable to store a function. This is different from function pointers.

std::function<return_type(arg1_type, arg2_type)> func_name;

Lambdas:

These are defined in place and temporary by nature.

// Syntax
[capture_list](parameter_list) -> return_type { function_body }
	// capture_list = parameters from surrounding scope
	//	that the lambda function body needs to access.
	// parameter_list = arguments to the lambda function
	// return_type = type of function return
	// function_body = body of lambda function, each line
	//	ending in semicolon.

// Accept a lambda as input to a function
void func(std::function<return_type(arg_type)> lambda_func) {
	return_type my_var;
	my_var = visitor(arg_type);
}
// Example
void func(std::function<std::string(int)> lambda_func) {
	std::string my_string;
	my_string = lambda_func(6);
}

Define a lambda with empty parameters (types only):

[var1, var2](int /* empty */, std::string /* empty */) -> return_type { function_body }

Declare a lambda variable, and call it:

// Save a lambda to a variable
std::function<return_type(arg1_type, arg2_type)> func_name;

// Call lambda
arg1_type = arg1;
arg2_type = arg2;
return_type my_result;
my_result = func_name(arg1, arg2);

Last updated 1 year ago