0

I am a beginner of C++. I am learning ADL (Augments Dependent Lookup). In my understanding, virtual_function_with_EnumInBase(B<T>::EnumInBase e) at line (1) can be looked up by ADL with B<T>::EnumInBase. However gcc 4.9.2 gives me error message:

virtual_function_with_EnumInBase was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation.

If someone knows the reason why it could not lookup the function or notices my misunderstanding, please tell me it. Thank you very much.

template<typename T>
class B {
public:
    virtual ~B() = default;
    enum EnumInBase { e0 = 0, e1 = 1 };
    virtual void virtual_function_with_EnumInBase(EnumInBase e) {
        std::cout << "B::virtual_function_with_EnumInBase(EnumInBase e)" << std::endl;
    }
};

template<typename T>
class D : public B<T> {
public:
    virtual ~D() = default;
    void virtual_function_test(void) {
        typename B<T>::EnumInBase val_for_lookup = B<T>::e0;
        virtual_function_with_EnumInBase(val_for_lookup);  // line (1)
    }
};

void test(void) {
    D<double> a;
    a.virtual_function_test();
}
owacoder
  • 4,815
  • 20
  • 47
mora
  • 2,217
  • 4
  • 22
  • 32

1 Answers1

2

You need to prefix call to virtual_function_with_EnumInBase with this, e.g. this->virtual_function_with_EnumInBase().

This is due to how name lookup works in templates. See Dependent names and Name Lookup, Templates, and Accessing Members of Base Classes for more details.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271