From 6ba2efe088b37e1a32bd575b459f941d419d64e4 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Fri, 17 Feb 2023 21:49:15 -0500 Subject: [PATCH] Simplify std::invoke example. --- CPP17.md | 19 ++++++++++--------- README.md | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/CPP17.md b/CPP17.md index bb41ca8..2764761 100644 --- a/CPP17.md +++ b/CPP17.md @@ -416,23 +416,24 @@ v; // == "trim me" ``` ### std::invoke -Invoke a `Callable` object with parameters. Examples of `Callable` objects are `std::function` or `std::bind` where an object can be called similarly to a regular function. +Invoke a `Callable` object with parameters. Examples of *callable* objects are `std::function` or lambdas; objects that can be called similarly to a regular function. ```c++ template class Proxy { - Callable c; + Callable c_; + public: - Proxy(Callable c): c(c) {} - template + Proxy(Callable c) : c_{ std::move(c) } {} + + template decltype(auto) operator()(Args&&... args) { // ... - return std::invoke(c, std::forward(args)...); + return std::invoke(c_, std::forward(args)...); } }; -auto add = [](int x, int y) { - return x + y; -}; -Proxy p {add}; + +const auto add = [](int x, int y) { return x + y; }; +Proxy p{ add }; p(1, 2); // == 3 ``` diff --git a/README.md b/README.md index cb40c71..cf6cac7 100644 --- a/README.md +++ b/README.md @@ -1085,23 +1085,24 @@ v; // == "trim me" ``` ### std::invoke -Invoke a `Callable` object with parameters. Examples of `Callable` objects are `std::function` or `std::bind` where an object can be called similarly to a regular function. +Invoke a `Callable` object with parameters. Examples of *callable* objects are `std::function` or lambdas; objects that can be called similarly to a regular function. ```c++ template class Proxy { - Callable c; + Callable c_; + public: - Proxy(Callable c): c(c) {} - template + Proxy(Callable c) : c_{ std::move(c) } {} + + template decltype(auto) operator()(Args&&... args) { // ... - return std::invoke(c, std::forward(args)...); + return std::invoke(c_, std::forward(args)...); } }; -auto add = [](int x, int y) { - return x + y; -}; -Proxy p {add}; + +const auto add = [](int x, int y) { return x + y; }; +Proxy p{ add }; p(1, 2); // == 3 ```