The following code produces different results when compiled using gcc 14.2 or clang 19.1:
namespace mylib { template <typename Type> void Function(const Type& t) { // Note: explicit Type:: qualification t.Type::Foo(); }}class Type { public: void Foo() const { std::cout << "Base function"; }};class DerivedType: public Type { public: void Foo() const { std::cout << "Derived function"; }};int main() { mylib::Function(DerivedType{});}
- in gcc 14.2: DerivedType::Foo() is called.
- in clang 19.1: Type::Foo() is called.
Any idea which behavior should be considered correct, if any?
Note: the two compilers produce the same output if class Type
is renamed to something different from the Function
template argument, in which case DerivedType::Foo
is called also by clang.