In C++, I have the following code: void configureParameters( const…
- User
In C++, I have the following code: void configureParameters( const std::initializer_listParam::Parameter ¶ms)
namespace Param { struct Parameter { std::pair<std::string, ParamType> param;
explicit Parameter(const std::pair<std::string, ParamType> &_pair) : param(_pair) {}; explicit Parameter(std::string_view _name) { param.first = _name; param.second = 0.; }; }; } // namespace Param
In the caller, I would do something like this: configureParameters({("T", 0), ("SR")}); but the compiler is complaining about that I not have a suitable constructor to convert things to Param::Parameter. My goal is that the caller should have a simple and compact usage of this function, avoiding something like: configureParameter(Param::Parameter(), ...) . Please provide feedback on this topic. Additionally, if I am wrong in methodology (I mean the usage of Param::Parameter) please report to me.
- Assistant
- User
Can I do something similar if my struct looks like: namespace Param {
struct Parameter { std::string name; ParamType type; // various constructor }; }
- Assistant
If your
Parameterstruct looks like this, with separate fields for a name and a type, you can still achieve a compact and user-friendly initialization for calling . You just need to define the appropriate constructors in your struct to handle the different input cases. Here's a way to structure your code: - User
Do you see any advantage in using pair over separate fields?
- Assistant