1

Lest consider the following code:

#include <iostream>

template <typename T_VAL>     // not used template argument
struct Foo {        int x;    };

template <typename T_VAL>
struct Bar {        int x;    };

template <typename T_VALUE>
struct A
{
    // just 2 overloaded data() members:

    virtual void data(Foo<T_VALUE>) {
        std::cout << "FOO EMPTY\n";
    }

    virtual void data(Bar<T_VALUE>) {
        std::cout << "BAR EMPTY\n";
    }
};


template <typename T_VALUE>
struct B : public A<T_VALUE>
{
    void data(Foo<T_VALUE>) {
        std::cout << "smart FOO impl\n";
    }
};


int main(void) {

    B<float> TheObject;
    Bar<float> bar;

    TheObject.data(bar); // I expect virtual base call here
    return 0;
}

The compiler message is:

tmpl.cpp: In function 'int main()':
tmpl.cpp:43:14: error: no matching function for call to 'B<float>::data(Bar<float>&)'
  TheObject.data(bar);
                    ^

void data(Bar<T_VALUE>) -> void data(Bar<T_VALUE>&) does not change anything

Why the derived class B has no virtual void data(Bar&)?

pavelkolodin
  • 2,859
  • 3
  • 31
  • 74

1 Answers1

6

The other data is hidden.

Do

template <typename T_VALUE>
struct B : public A<T_VALUE>
{
    using A<T_VALUE>::data; // Add this line
    void data(Foo<T_VALUE>) {
        std::cout << "smart FOO impl\n";
    }
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302