Reputation: 6286
I recently came across an instance in which I realized that several classes I had written shared a lot of functionality. I realized I could generalize these classes into a common base with some template parameters. The problem is that these classes have about 20 different overloaded constructors, where most of the classes functionality is implemented. Using I came up with the solution illustrated here:
template <typename T>
class Base {
protected:
T member_;
public:
Base(int a) { /* ... */ }
Base(double a) { /* ... */ }
Base(char a) { /* ... */ }
Base(int a, double b, char c) { /* ... */ }
// etc...
};
class Derived1 : public Base<int> {
public:
template <typename... Args>
Derived1(Args... args) : Base(args...) { }
// other constructors specific to Derived1
};
class Derived2 : public Base<char> {
public:
template <typename... Args>
Derived2(Args... args) : Base(args...) { }
// other constructors specific to Derived2
};
I have several questions here:
std::forward
in the derived classes, right?)explicit
keyword. Why?)Upvotes: 1
Views: 857
Reputation: 42544
Yes, you should be using perfect forwarding. If the constructors are declared inline, there will be zero performance impact.
It is a "scary-looking template," but easily recognized by someone who knows anything about templates. Depends on your maintainers, I guess?
You need to beware of intercepting calls intended for the derived class's copy and move constructor, since your variadic perfect forwarding signature matches everything. Properly constraining with enable_if
usually requires more code than the forwarding constructor itself.
Inheriting constructors is the "better" way to do this. The feature is specified to avoid capturing signatures that should go to the derived class constructors. Compiler support may be iffy: it's not the easiest feature to implement, or the most in-demand, so implementors put it off for quite a while.
Upvotes: 3
Reputation: 5063
In C++11 , constructors can be inherited.
See https://stackoverflow.com/a/434784/232574
Upvotes: 4