Categories
Programming

STL transform() meets constructors

Using the STL transform() function you can apply “function like things” to a collection to make a new collection. That means anything which can be called by sticking brackets after it, so functions are fine and function objects are fine.

But not all “function like things” work. What happens if you have a collection of ints, and you want to transform them into a collection of Foo’s, where the constructor for class “Foo” takes a single integer argument.

Well, you can’t just pass “Foo” to transform() because that’s a type, not a value. You can’t pass Foo::Foo either, It doesn’t work.

You can create a static method, say Create(int i), which just builds a Foo object and returns it. That works. But it’s incredibly inelegant.