I have heard that C++ class member function templates can\'t be virtual. Is this true?
If they can be virtual, what is an example of a scenario in which one would
At least with gcc 5.4 virtual functions could be template members but has to be templates themselves.
#include
#include
class first {
protected:
virtual std::string a1() { return "a1"; }
virtual std::string mixt() { return a1(); }
};
class last {
protected:
virtual std::string a2() { return "a2"; }
};
template class mix: first , T {
public:
virtual std::string mixt() override;
};
template std::string mix::mixt() {
return a1()+" before "+T::a2();
}
class mix2: public mix {
virtual std::string a1() override { return "mix"; }
};
int main() {
std::cout << mix2().mixt();
return 0;
}
Outputs
mix before a2
Process finished with exit code 0