New to modern C++, trying to figure out what's going on here. Following code compiles fine:
template<int base_num>
class Base{
public:
Base() {}
};
template<int derived_num>
class Derived : public Base<0> { // Integer literal passed to base template
public:
Derived(): Base() {}
};
Whereas the following:
template<int base_num>
class Base{
public:
Base() {}
};
template<int derived_num>
class Derived : public Base<derived_num> { // Non-type template parameter passed to base template
public:
Derived(): Base() {}
};
Fails with:
class ‘Derived<derived_num>’ does not have any field named ‘Base’
I'm speculating that this happens because derived_num
itself doesn't count as a "structural type", but unsure. Hoping someone can explain what is going on here and if there is a way to "pass-through" non-type template parameters to a base class template.
Update:
Explicitly defining the template "fixes" this
template<int derived_num>
class Derived : public Base<derived_num> {
public:
Derived(): Base<derived_num>() {}
};
But becomes very budernsome for more complex base templates and multiple constructors. If it can be avoided I would like to, and I still don't understand why this fix works either.
Update 2:
The problem code fails with GCC and clang, but works on MSVC. Compiler bug? (Who's bugged, GCC/Clang or MSVC?) Invoking an MSVC extension? Very out of my element now, will wait patiently for a standards maven to tell me what's supposed to happen here.