Search code examples
c++c++11using

Use cases of keyword using in C++11


I know using in C++11 behaves same as typedef. I have this code and found different use cases:

template<typename T, int a>
class Base
{
public:
     std::vector<T> noise_(a);
     using VectorType = std::vector<T>;
     virtual VectorType getVector() const
     {
        return noise_;
     }
protected:
     VectorType noise_;
};

template<typename T, int a> 
class Derived : public Base<T,a>
{
public:
    using Base<T,a>::noise_;
    using VectorType = typename Base<T,a>::VectorType; 
    using Base<T,a>::getVector;
};

Here, using is used in 3 different way. What is the purpose of the following line (noise_ is a protected member of the base class):

using Base<T,a>::noise_;

Same for:

using Base<T,a>::getVector;

Solution

  • Simply put, when the base class depends on a template parameter, its scope is not inspected to resolve names. Hence, you cannot refer to noise_ in Derived using just noise_. You should either write this->noise_, or introduce the name with using.