2015-09-24 - when order does not matter

after quite a long break i finally share presentation materials from NDC Oslo: presentation and recording. unfortunately first few minutes of the recording are lacking video and video is in a different format. fortunately it is still readable, so one knows what's going on. :)

during presentation i show how to implement a C++ function, that can take arguments in any order, and provide arguments for those, that are not given. for instance having function foo:

void foo(std::string abc="test", int answer=42, std::vector<double> values={1.0,2.0});

instead of having code like this:

foo(std::string{"other"}, /* how to provide default value here?! */, std::vector<double>{1.1, 2.2});

one can write something like this:

foo(std::string{"other"}, std::vector<double>{1.1, 2.2});

…or like this:

foo(std::vector<double>{1.1, 2.2}, std::string{"other"});

…or like this:

foo(std::vector<double>{1.1, 2.2});

…etc. i think you see the point now. :)

why would you need something like this? in fact it proves to be very useful for testing frameworks, when you have common function calls, that can take many arguments, but typically users pass only some of them, keeping remaining as default/non-interesting. having such info, one can write nice comparators, that allow to ignore non-provided values! this make users' code very clean and, as a free lunch, does not require them to remember “the order” of parameters.

i was thinking about writing an article about it, but taking into consideration how much overbooked calendar i have now (this is my first post in 4 months!) it ain't gonna happen anywhere soon.

blog/2015/09/24/when_order_does_not_matter.txt · Last modified: 2015/09/24 15:26 by basz
Back to top
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0