When writing a wrapper class in C++, it's nice to be able to forward arguments to constructors using parameter packs. But doing the Simplest Thing That Could Possibly Work has some drawbacks.
// BAD!
template <class ... Types>
wrapper(Types&&... args) : wrapped(std::forward<Types>(args)...) {}
From what I can gather, this is bad because it can shadow special member functions. Shadowing special member functions and the associated problems with doing that are discussed in many places including here and here. There are also problems related to explicit
-ness or initializer lists as described in one of the comments to this question.
However, it seems to me like these problems do not apply to the case of constructors with >=2
arguments.
// fine?
template <class FirstType, class SecondType, class... Types>
wrapper(FirstType && first,
SecondType && second,
Types &&... args) :
wrapped(
std::forward<FirstType>(first),
std::forward<SecondType>(second),
std::forward<Types>(args)...
) {
// do nothing
}
Is there any drawback to mindlessly forwarding all >=2-argument constructors using a naive wrapper like the one above and only using a complex, SFINAE-heavy, carefully written wrapper for unary constructors? (I'm assuming the special case where the wrapper's constructor is called with no arguments can be handled specially fairly trivially.)
Aucun commentaire:
Enregistrer un commentaire