From 955dd950f0607b0c85cdaf82391235b6710c999c Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Sat, 4 Feb 2017 08:45:00 -0500 Subject: [PATCH] C++11 compliance on older compilers --- include/CLI.hpp | 16 +++++++--------- tests/CLITest.cpp | 2 +- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/include/CLI.hpp b/include/CLI.hpp index 7f6a4c25..ec8880c4 100644 --- a/include/CLI.hpp +++ b/include/CLI.hpp @@ -603,24 +603,22 @@ protected: bool parsed{false}; App* subcommand = nullptr; - std::function app_callback; + std::function app_callback; public: - /// Set a callback to run at the end of parsing - App* set_callback(std::function callback) { - app_callback = callback; - return this; - } - /// Don't have to worry about explicit App* in argument + /// Set a callback for the end of parsing. Due to a bug in c++11, + /// it is not possible to overload on std::function (fixed in c++14 + /// and backported to c++11 on newer compilers). Use capture by reference + /// to get a pointer to App if needed. App* set_callback(std::function callback) { - app_callback = [callback](App*){callback();}; + app_callback = callback; return this; } void run_callback() { if(app_callback) - app_callback(this); + app_callback(); } /// Reset the parsed data diff --git a/tests/CLITest.cpp b/tests/CLITest.cpp index 4f98e652..d1bf22e6 100644 --- a/tests/CLITest.cpp +++ b/tests/CLITest.cpp @@ -383,7 +383,7 @@ TEST_F(TApp, BasicSubcommands) { TEST_F(TApp, Callbacks) { auto sub1 = app.add_subcommand("sub1"); - sub1->set_callback([](CLI::App*){ + sub1->set_callback([](){ throw CLI::Success(); }); auto sub2 = app.add_subcommand("sub2");