1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 12:43:52 +00:00

C++11 compliance on older compilers

This commit is contained in:
Henry Fredrick Schreiner 2017-02-04 08:45:00 -05:00
parent 8b224fa7a9
commit 955dd950f0
2 changed files with 8 additions and 10 deletions

View File

@ -603,24 +603,22 @@ protected:
bool parsed{false}; bool parsed{false};
App* subcommand = nullptr; App* subcommand = nullptr;
std::function<void(App*)> app_callback; std::function<void()> app_callback;
public: public:
/// Set a callback to run at the end of parsing
App* set_callback(std::function<void(App*)> 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<void()> callback) { App* set_callback(std::function<void()> callback) {
app_callback = [callback](App*){callback();}; app_callback = callback;
return this; return this;
} }
void run_callback() { void run_callback() {
if(app_callback) if(app_callback)
app_callback(this); app_callback();
} }
/// Reset the parsed data /// Reset the parsed data

View File

@ -383,7 +383,7 @@ TEST_F(TApp, BasicSubcommands) {
TEST_F(TApp, Callbacks) { TEST_F(TApp, Callbacks) {
auto sub1 = app.add_subcommand("sub1"); auto sub1 = app.add_subcommand("sub1");
sub1->set_callback([](CLI::App*){ sub1->set_callback([](){
throw CLI::Success(); throw CLI::Success();
}); });
auto sub2 = app.add_subcommand("sub2"); auto sub2 = app.add_subcommand("sub2");