Boost Parameters Library

I recently found Settings in the Boost library. Honestly, I did not understand the reasons why this is part of Boost. When you need to pass several parameters to a function, you can create a structure from them, for example:

struct Parameters { Parameters() : strParam("DEFAULT"), intParam(0) {} string strParam; int intParam; }; void foo(const Parameters & params) { } Parameters params; params.intParam = 42; foo(params); 

It is very easy to write and understand. Now an example using the boost parameters:

 BOOST_PARAMETER_NAME(param1) BOOST_PARAMETER_NAME(param2) BOOST_PARAMETER_FUNCTION( (void), // 1. parenthesized return type someCompexFunction, // 2. name of the function template tag, // 3. namespace of tag types (optional // optional parameters, with defaults (param1, *, 42) (param2, *, std::string("default")) ) ) { std::cout << param1 << param2; } someCompexFunction(param1_=42); 

I think this is really difficult, and the advantage is not so important.

But now I see that some of the Boost (Asio) libraries use this technique. Is it recommended to use this library to pass many arguments?

Or maybe there is real benefit from using this library that I don't see? Do you recommend using this library in a project?

+4
source share
2 answers

Your technique requires the creation of a large number of time parameters) and in some cases it will be rather verbose. Something that is even more complicated documentation. If you go down the configuration route, you will have two places where you need to explain your options. Documenting Boost.Parameter functions is easy to compare.

It also holds verbosity and allows me to use arguments for entire families of functions instead of compiling a new media configuration over and over.

If you do not like the library, do not use it. It has several other drawbacks that you did not mention (heavy includes, high compilation time).

Also, why not just provide the best of both worlds? One function using Boost.Parameters and the other using configuration structures, where both are sent to a common implementation. Manage your headings correctly, and the promise of "don’t pay for what you don’t use" will be kept. Price - maintainability. But you can always refuse one interface if your users do not like it.

+2
source

Well, I do not use this library, but the key is that you can pass parameters by name.

Imagine that you have a function with many parameters, and in most cases you want to use only a few. Maybe it’s not always the same, so putting them in front of the list (so that the rest can be provided as default values) will not help. The one that includes the named parameter element: you simply specify the names and values ​​of the parameters you want to pass in whatever order you like, and the rest will be by default. You do not even need to know all the possible parameters; a later version of the function can add new parameters without violating anything (provided that the default values ​​for the new parameters are selected to simulate the old behavior).

Compared to structures, you can create a structure and initialize everything by default. This is very similar to how this stuff works internally, if I am not mistaken in passing the parameter object and setting the values ​​there before passing it to the actual function at the end.

+1
source

All Articles