levanovd
levanovd

Reputation: 4165

Eclipse CDT can not resolve method in template class

I have an issue with Eclipse CDT indexer/code analyzer. When I write the following code in the editor, it shows me an error on const int* p = x.f(); - Invalid arguments Candidates are: int * f(). So for some reason it doesn't recognize method const T* f() const in class B.

template<typename T>
class A {
public:
    const T* f() const { return 0; }
};

template<typename T>
class B : A<T> {
public:
    using A<T>::f;
    T* f() { return 0; }
};

void main() {
    const B<int> x;
    const int* p = x.f();
}

Any ideas why does it happen and how to solve the problem?

Upvotes: 1

Views: 1509

Answers (1)

Mike Kinghan
Mike Kinghan

Reputation: 61515

Why is happening?

Because the CDT C++ parser is failing to recognize that using A<T>::f declares in class B a member function that satisfies the call const int* p = x.f() in main(). You can verify this by adding a const qualifier to T* B::f(). This satisfies the parser (but defeats the purpose of the member function).

How to solve the problem?

Well, it does seem peculiar to have const T* f() const in a base class and T* f() in a derived class. Unless there is some compelling reason for this, you could declare both member functions in either A or B (probably A). Alternatively, but unattractively, you could:

  • Give them different names, maybe f and f_const, or
  • Remove using A<T>::f; and replace const int* p = x.(); with the explicitly qualified call const int* p = x.A<int>::f();

You could also ignore the problem, since the parser's confusion doesn't stop your program building successfully in the IDE. (Nice to have a realtime C++ parser, except when it doesn't agree with your compiler).

Upvotes: 1

Related Questions