This code works:
struct Defs
{
static const int a = 1;
int b{};
void g() {}
};
struct Bob : Defs
{
void f()
{
cout << a << "\n";
cout << b << "\n";
g();
}
};
int main()
{
Bob b;
b.f();
}
But this code doesn't:
struct Defs
{
static const int a = 1;
int b{};
void g() {}
};
template<class D>
struct Bob : D
{
void f()
{
cout << a << "\n";
cout << b << "\n";
g();
}
};
int main()
{
Bob<Defs> b;
b.f();
}
Errors:
prog.cpp: In member function 'void Bob<D>::f()':
prog.cpp:16:11: error: 'a' was not declared in this scope
cout << a << "\n";
^
prog.cpp:17:11: error: 'b' was not declared in this scope
cout << b << "\n";
^
prog.cpp:18:5: error: there are no arguments to 'g' that depend on a template parameter, so a declaration of 'g' must be available [-fpermissive]
g();
^
prog.cpp:18:5: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
But if I do the following, it works:
template<class D>
struct Bob : D
{
void f()
{
cout << D::a << "\n";
cout << D::b << "\n";
D::g();
}
};
Is it possible to get a class to use the members of a base class provided as a template parameter, without qualifying them? The reason I ask is because doing so would allow me to refactor some code without a LOT of changes.
It can be assumed the type used as the template parameter has all those members, otherwise a compile failure is acceptable.