Why is a member in a primary class template not declared in the scope of the specialized class?

Viewed 95

Please be aware that you will see an oversimplified version of the code here. So it might look useless. However, that is not the problem.

The problem is that I have a primary template class like this:

// The primary template class
template<int N>
struct Start {
  int start;

  template<typename... Args>
  int operator()(Args... args) const {return start;}
};

When I specialize this class template:

// Specialization for N=1:
template<>
struct Start<1> {
  int operator()(int i) const {return start;}
};

I get the following error:

error: ‘start’ was not declared in this scope
int operator()(int i) const {return start;}

Why is start not declared in the scope of class Start<1>?

Question

  • Why does the specialized class not recognize the member of the primary class?

I appreciate any suggestions.

1 Answers

Template specialization is not the same as inheritance.

Your specialization is a completely different type from the primary template. No data members or functions are shared.

If you want to share a common member, then make both types inherit from a common base class.

Related