1

In the example below, the using declarations from the base class are only inherited by the derived class that is a full specialization of the base class. I was surprised by that at first. Is this because "members of partial specializations are not related to the members of the primary template"? Is that necessary or strongly motivated by some use-case I haven't considered?

Additionally, that would make me expect that the member functions should also be unrelated. If that's the case, why doesn't use of the override keyword cause issues?

template <typename T, typename U>
class Base {
 public:
  virtual ~Base() = default;

  using TPtr = const T*;
  using UPtr = const U*;

  virtual void TFunc(TPtr t);
  virtual void UFunc(UPtr u);
};

template <typename T>
class PartiallySpecialized : Base<T, int> {
 public:
  // These aren't inherited. 
  using BaseT = Base<T, int>;
  using typename BaseT::TPtr;
  using typename BaseT::UPtr;

  // These are inherited?
  virtual void TFunc(TPtr t) override {}
  virtual void UFunc(UPtr u) override {}
};

class FullySpecialized : Base<double, int> {
 public:
  virtual void TFunc(TPtr t) override {}
  virtual void UFunc(UPtr u) override {}
};

int main() {
  PartiallySpecialized<double> p;
  FullySpecialized f;
}

rkeatin3
  • 88
  • 6
  • 3
    Your code does not contain any specialization of any template. – StoryTeller - Unslander Monica Aug 30 '21 at 21:50
  • 1
    It has to do with dependent name resolution. See [this question](https://stackoverflow.com/questions/4643074/why-do-i-have-to-access-template-base-class-members-through-the-this-pointer) for an explanation of why. – 1201ProgramAlarm Aug 30 '21 at 22:36
  • our question is very difficult for me to follow. You use wrong terminology. I don't understand what you expected to happen. "// These aren't inherited. " Inherited by who? There is no class clas derives that. " // These are inherited?" Again by who? There is no class that derives that. – bolov Aug 30 '21 at 22:44
  • @StoryTeller-UnslanderMonica, perhaps I'm using the wrong terminology, but a declaration of struct `Base` would be a partial specialization, would it not? – rkeatin3 Aug 30 '21 at 23:53
  • @bolov, `PartiallySpecialized` derives from `Base`. I expected that a using declaration from the base class would be "inherited" by the derived class. It seem I was wrong (hence the need for new using declarations). – rkeatin3 Aug 30 '21 at 23:57
  • 1
    a template specialization add a specialized definition that differs from the base template. You are not not doing that. The difference between the two is that one is a template and the base class is dependent on the template parameter and the other is a class with the base class non-dependent. The full answer is in the dupe. – bolov Aug 31 '21 at 00:48
  • I see. `PartiallySpecialized` undergoes the two-phase compilation, so both `TPtr` and `UPtr` must be made dependent types in order to pass the first phase of compilation (which comes before checks on instantiations). On the otherhand, `FullySpecialized` doesn't undergo two-phase compilation, and when instantiated, the using declarations unambiguously refer to the declarations from the base class. – rkeatin3 Aug 31 '21 at 02:13

0 Answers0