Shorter syntax for using a class constructor as a transform function.
Hi Everyone, I would like to check if something like the following would be a useful addition to Boost.Core, or if something like this maybe exists: I have a class like ``` struct X { std::string str; explicit X(const std::string& s) : str(s) {} }; ``` I need to transform a vector of strings into a vector of X through invoking the constructor of X: ``` std::vectorstd::string strings = {"cat", "dog", "emu", "fox"}; std::vector<X> xs; std::transform(strings.begin(), strings.end(), std::back_inserter(xs),[](auto const& s) { return X(s); })); ``` It works, but a shorter version would be to introduce a component that is equivalent to the lambda: ``` std::transform(strings.begin(), strings.end(), std::back_inserter(xs), construct<X>)); ``` The full example: https://godbolt.org/z/85YoP9Pa9 Regards, &rzej;
On Mon, Sep 30, 2024 at 6:20 PM Andrzej Krzemienski via Boost
I need to transform a vector of strings into a vector of X through invoking the constructor of X:
FYI, related SO post: https://stackoverflow.com/questions/65991331/best-way-to-use-constructor-as-...
pon., 30 wrz 2024 o 19:03 Dominique Devienne
On Mon, Sep 30, 2024 at 6:20 PM Andrzej Krzemienski via Boost
wrote: I need to transform a vector of strings into a vector of X through invoking the constructor of X:
FYI, related SO post:
https://stackoverflow.com/questions/65991331/best-way-to-use-constructor-as-...
Thanks. Maybe ranges solve this for std::transform. My original problem was in fact reported in the context of boost::optional::map. Regards, &rzej;
El 30/09/2024 a las 18:20, Andrzej Krzemienski via Boost escribió:
Hi Everyone, I would like to check if something like the following would be a useful addition to Boost.Core, or if something like this maybe exists:
I have a class like ``` struct X { std::string str; explicit X(const std::string& s) : str(s) {} }; ```
I need to transform a vector of strings into a vector of X through invoking the constructor of X:
``` std::vectorstd::string strings = {"cat", "dog", "emu", "fox"}; std::vector<X> xs;
std::transform(strings.begin(), strings.end(), std::back_inserter(xs),[](auto const& s) { return X(s); })); ```
It works, but a shorter version would be to introduce a component that is equivalent to the lambda:
You can use boost::value_factory: ``` std::vectorstd::string strings = {"cat", "dog", "emu", "fox"}; std::vector<X> xs; std::transform( strings.begin(), strings.end(), std::back_inserter(xs), boost::value_factory<X>()); ``` Joaquin M Lopez Munoz
Andrzej Krzemienski wrote:
Hi Everyone, I would like to check if something like the following would be a useful addition to Boost.Core, or if something like this maybe exists:
I have a class like ``` struct X { std::string str; explicit X(const std::string& s) : str(s) {} }; ```
I need to transform a vector of strings into a vector of X through invoking the constructor of X:
``` std::vectorstd::string strings = {"cat", "dog", "emu", "fox"}; std::vector<X> xs;
std::transform(strings.begin(), strings.end(), std::back_inserter(xs),[](auto const& s) { return X(s); })); ```
It works, but a shorter version would be to introduce a component that is equivalent to the lambda:
``` std::transform(strings.begin(), strings.end(), std::back_inserter(xs), construct<X>)); ```
This exists at least four times in Boost (in Lambda, Hof, Phoenix, Functional/Factory), probably more that I haven't thought of. But it's not in Core (or in Bind). (Not in Lambda2 either despite https://github.com/boostorg/lambda2/issues/6) We try to not make Core the repository of every useful thing someone thinks of; this role was served by Utility in the past, or Functional for function objects, but these multiple unrelated component repositories never work out that well.
participants (4)
-
Andrzej Krzemienski
-
Dominique Devienne
-
Joaquin M López Muñoz
-
Peter Dimov