r/cpp • • Apr 19 '20

Default function arguments are the devil

https://quuxplusone.github.io/blog/2020/04/18/default-function-arguments-are-the-devil/
90 Upvotes

149 comments sorted by

View all comments

2

u/Omnifarious0 Apr 19 '20 edited Apr 19 '20

Don't do this:

void doTask(Task task, std::seconds timeout, bool detach = false, bool captureStdout = false, bool captureStderr = false);

Do this:

#include <chrono>

struct Task {
};

struct task_params {
    bool const detach = false;
    bool const captureStdout = false;
    bool const captureStderr = false;
};

void doTask(Task task, std::chrono::seconds timeout, task_params flag = {});

void test()
{
    using namespace std::literals::chrono_literals;

    doTask(Task{}, 5s, { .detach=true, .captureStderr=true});
}