Object Generator
More C++ Idioms/Object Generator
Intent
To simplify creation of objects without explicitly specifying their types. (This is not the factory method pattern)
NOTE: factory method是OOP的design pattern
Solution and Sample Code
In the object generator idiom, a template function is created whose only job is to construct a new object from its parameters. It is based on a useful property of function templates which class templates don't have: The type parameters of a function template are deduced automatically from its actual parameters.
NOTE: 充分运用function template的automatically deduce parameter特性,进而实现简化代码的目的
template <class T, class U>
std::pair <T, U>
make_pair(T t, U u)
{
return std::pair <T, U> (t,u);
}
The C++ standard library defines several object generators to avoid code bloat. std::bind2nd and std::mem_fun are two such standard object generators that can be used to avoid code bloat in the example shown in the motivation section above.
void read_lines(std::vector<File *> files)
{
std::string arg;
for_each(files.begin(), files.end(), bind2nd(mem_fun(&File::read_line), arg));
}
Known Uses
References
NOTE: 这个链接到的是boost Generic Programming Techniques#TYPE GENERATOR,我是在阅读这篇文章的时候,发现了这个idiom的。
Replacement: C++17 Class template argument deduction (CTAD)
在阅读stackoverflow Current status of std::make_array # A
C++17 includes template argument deduction for classes, so you can now write:
std::pair p (foo, bar); std::array arr = { 1, 2, 3, 4, 5 };
and so on. But there are some (somewhat subtle) remaining use cases where
make_pair
ormake_array
can be useful, and you can read about them in: Usefulness of std::make_pair and std::make_tuple in C++1z
显然 C++17 Class template argument deduction (CTAD) 在一定程度上可以取代 object generator pattern。