C++: Inheritance from template parameter

user890739

In the next code example:

#include <iostream>
using namespace std;
int f() {
    return 0;
}
struct A {
    int f()    {
        return 1; }
};
template<class T>
struct C : public T {
    C() {
        cout << f() << endl;
    } };
int main() {
    C<A> c; // prints 0
    return 0;
}

If I change the inheritance to be non-template like this: struct C : public A

then it prints "1" and not 0.

Why is that?

T.C.

in f(), f is a nondependent name, so name lookup happens at template definition time (before T is known) and binds it to ::f. If you want it to call the member function f, then use this to make it a dependent name:

template<class T>
struct C : public T {
    C() {
        cout << this->f() << endl;
    } 
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Template inheritance: There are no arguments that depend on a template parameter

From Dev

C++ Template Inheritance

From Dev

C++: Inherit class from template parameter

From Dev

C++ generic template method name from template parameter

From Dev

C++: template class inheritance with variable-type parameters using parameter packs

From Dev

Non-template Inheritance from a template

From Dev

Inheritance and template function c++

From Dev

C++ template specialisation & inheritance

From Dev

C++ template class inheritance

From Dev

c++ template inheritance scheme

From Dev

c++ template abstract inheritance

From Dev

c++: Calling a template method of a base class from a template child class though multilevel inheritance

From Dev

Template class in c++ inheritance in template

From Dev

C# inheritance override a parameter

From Dev

C++ Template Parameter that evaluates a Template (template template parameter)

From Dev

Compile time array from C++ template parameter pack

From Dev

c++ Unpacking parameter pack from template arguments

From Dev

Template int parameter from constant expression in C++11

From Dev

Determining a template pack from multiple inheritance

From Dev

template inheritance from multiple sub-templates

From Dev

The non-type template parameter of pointer to overloaded member functions with inheritance

From Dev

The non-type template parameter of pointer to overloaded member functions with inheritance

From Dev

C++ Inheritance: Calling subclass function of template

From Dev

C++ template partial specialization with inheritance

From Dev

C++11 variadic template + inheritance

From Dev

C++ template subclass and multiple inheritance ambiguity

From Dev

insert an template object in vector with inheritance c++

From Dev

C++11 Cannot cast template inheritance

From Dev

C++ template declaration with inheritance list

Related Related

  1. 1

    Template inheritance: There are no arguments that depend on a template parameter

  2. 2

    C++ Template Inheritance

  3. 3

    C++: Inherit class from template parameter

  4. 4

    C++ generic template method name from template parameter

  5. 5

    C++: template class inheritance with variable-type parameters using parameter packs

  6. 6

    Non-template Inheritance from a template

  7. 7

    Inheritance and template function c++

  8. 8

    C++ template specialisation & inheritance

  9. 9

    C++ template class inheritance

  10. 10

    c++ template inheritance scheme

  11. 11

    c++ template abstract inheritance

  12. 12

    c++: Calling a template method of a base class from a template child class though multilevel inheritance

  13. 13

    Template class in c++ inheritance in template

  14. 14

    C# inheritance override a parameter

  15. 15

    C++ Template Parameter that evaluates a Template (template template parameter)

  16. 16

    Compile time array from C++ template parameter pack

  17. 17

    c++ Unpacking parameter pack from template arguments

  18. 18

    Template int parameter from constant expression in C++11

  19. 19

    Determining a template pack from multiple inheritance

  20. 20

    template inheritance from multiple sub-templates

  21. 21

    The non-type template parameter of pointer to overloaded member functions with inheritance

  22. 22

    The non-type template parameter of pointer to overloaded member functions with inheritance

  23. 23

    C++ Inheritance: Calling subclass function of template

  24. 24

    C++ template partial specialization with inheritance

  25. 25

    C++11 variadic template + inheritance

  26. 26

    C++ template subclass and multiple inheritance ambiguity

  27. 27

    insert an template object in vector with inheritance c++

  28. 28

    C++11 Cannot cast template inheritance

  29. 29

    C++ template declaration with inheritance list

HotTag

Archive