1

What is the point of the following using-declarations

using eoPop<MOEOT>::size;
using eoPop<MOEOT>::operator[];
using eoPop<MOEOT>::back;
using eoPop<MOEOT>::pop_back;

Taken from the class defined here. Surely, because eoPop<EOT> inherits std::vector<EOT>, the methods size, operator[] , back and pop_back are public there is no need for the using declaration. Are the using declaration used to force instantiation?

1
  • Btw. I haven't seen this technique before; you could also use this->size() etc. to find those member functions. It's an interesting way. Commented Oct 12, 2013 at 21:39

1 Answer 1

2

Although I don't know why they've decided to include those using-declarations, I can tell why the code would be ill-formed without them.

But first, repeating from a comment:

A using-declaration doesn't require the existence of the definition the nominated entity (here: it doesn't require the existence of the definition of those functions). Consider:

void foo();

int main()
{
    using ::foo; // doesn't require the definition of `foo` to exist
    return 0;
}

If a class template gets implicitly instantiated, the declarations of its member functions are instantiated, but not their definitions (as per [temp.inst]/1). The base classes of a class template are also instantiated if said class template is implicitly instantiated (which in turn leads to the instantiation of the declarations of the member functions of those base class templates). Therefore, the using-declaration doesn't help with instantiation.


An effect of those using-declarations is that the names declared are visible for non-dependent name lookup. As per [temp.dep]/3

In the definition of a class or class template, if a base class depends on a template-parameter, the base class scope is not examined during unqualified name lookup [...].

In the linked code, we find for example i<size(). The name size here is non-dependent, therefore the base class scope is not searched, and eoPop < MOEOT > :: size would not be found without the using-declaration.

Another reason to use using-declarations is if you want to overload a member function of a base class. If you don't use the using-decl, the member function in the derived class simply hides every overload (with the same name) in the base class. As far as I can see, this is not used in the linked code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! 'Didn't know that overloading a method in a derived class will hide all other methods in that have the same name in the base class!! Don't underestimate the ability of C++ to embarass you :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.