user3541297
user3541297

Reputation: 13

How to define a template of template class

I' ve got a template class :

template <class T>
class A 
{
 ...
 protected:
  T m_value;
}

And i would like te make a template using this class for vectors:

template <class T>
class B:public A<std::vector<T>> //no need of space between >> (c++11)
{
 void testSize()
 {
    if(m_value.size() > ...)
    {
        ...
    }
 }
}

The compiler complain about : error: 'm_value' was not declared in this scope

is there a way i can do this or have i to recode this function for each std::vector type using directly the A class?

Thanks,

Edit:

I've tryed this:

template <class T>
class B:public A<std::vector<T>> //no need of space between >> (c++11)
{
 void testSize()
 {
    if(m_value.size() > ...)
    {
        ...
    }
 }

 std::vector<T> m_value;
}

The compiler doesn't complain anymore but does the m_value called in class A function's refer to m_value of class B?

Upvotes: 1

Views: 119

Answers (1)

skypjack
skypjack

Reputation: 50540

In your first example, m_value is a dependent name.
Just do this to correctly refer it from within B:

this->m_value.size()

That is, turn your if statement to:

if(this->m_value.size() > ...)

The code in the edit section is wrong instead. Class B and class A will refer respectively to their own copy of m_value.

Upvotes: 3

Related Questions