If I have a class with a unary constructor, can I somehow use that constructor as a UnaryOperator in an algorithm? i.e.:
#include <algorithm>
#include <vector>
class Thing
{
public:
Thing(int i)
: m_i(i)
{}
int i() {
return m_i;
};
private:
int m_i;
};
int main() {
std::vector<int> numbers{0,4,-13,99};
std::vector<Thing> things;
std::transform(numbers.begin(), numbers.end(), std::back_inserter(things), Thing);
}
the above code doesn't work, the code below works but I'm wondering if there's a better way:
#include <algorithm>
#include <vector>
class Thing
{
public:
Thing(int i)
: m_i(i)
{}
int i() {
return m_i;
};
private:
int m_i;
};
int main() {
std::vector<int> numbers{0,4,-13,99};
std::vector<Thing> things;
auto make_thing = [](auto&& i){return Thing(i);};
std::transform(numbers.begin(), numbers.end(), std::back_inserter(things), make_thing);
}