Consider the following class:
class Person {
public:
// I don't want any "char *" to be converted to Person implicitly!
explicit Person( const char * name ) : name_(name) {};
private:
std::string name_;
};
Also consider following array of char* data:
char STUDENT_NAMES[][20] = {
"Bart",
"Liza",
"Maggie"
};
Now, I want to create std::list of Person according to this array. All I could invent is to use std::transform algorithm with hand-written function object:
struct CreatePerson : public std::unary_function<const char*,Person> {
Person operator() (const char * name) const {
return Person(name);
};
};
// ...
std::list<Person> students;
std::transform(
&STUDENT_NAMES[ 0 ],
&(STUDENT_NAMES[ sizeof(STUDENT_NAMES)/sizeof(STUDENT_NAMES[0]) ]),
front_inserter(students),
CreatePerson() );
// ...
Is there any shorter and/or clearer way to do it? Maybe some standard function objects or adaptors?