This code sample works and doing exactly what it is supposed to do.
template <typename T>
struct base {
base(T a) {};
};
struct b : public base<int> {
b() : base(3) {};
};
I want to utilize C++17 feature with auto-deduction template parameters from constructor and do something like this:
struct b : public base { // note lack of <int>
b() : base(3) {};
};
Is it possible at all without some ugly hacks?
The template argument deduction rules don't allow for it. And it's unlikely they ever will. Consider this:
struct b : public base { // note lack of <int>
b() : base(3) {}
b(int) : base(false) {}
};
What is the base class of b
now? One may argue that it's feasible if all initializations of the base class agree on the type. But it's hard to check that in practice (think separate object files where c'tors reside), and it seems IMO too niche of a feature to be useful.
So, to summarize, you can't avoid specifying the template arguments for the base class template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With