0

I have an issue that is already known in the community (one of the most complete answers can be found here), but I would like to know if there is a way to make compiler understand that you want to access base template members without needing to use typedef or this as below:

#include <iostream>

template<typename T>
struct Base{
  T a;
  T a2;
};

template<typename T1, typename T2>
struct Derived : Base<T2>{
  T2 b;

  void member_fnc();
};

template<typename T1, typename T2>
void Derived<T1,T2>::member_fnc(){
  typedef Base<T2> base;
  std::cout << "a = " << base::a << std::endl;    // or this->a;
  std::cout << "a2 = " << base::a2 << std::endl;  // or this->a2;                     
  std::cout << "b = " << b << std::endl;
}

int main(){

  Derived<int,double> der1;
  der1.a = 1;
  der1.a2 = 2;
  der1.b = 1.1;
  der1.member_fnc();

}

I have this feeling that there should be a way to say compiler you want to access base template Base<T2> without having to type it or including using for every member you want to access, as you can unnest namespaces using using keyword, but I couldn't find so far. Is it possible, at all?

Community
  • 1
  • 1
Werner
  • 2,537
  • 1
  • 26
  • 38

1 Answers1

1

Why not:

template<typename T1, typename T2>
struct Derived : Base<T2>{
  typedef Base<T2> base;
  using base::a;
  using base::a2;

  T2 b;
  void member_fnc();
};

template<typename T1, typename T2>
void Derived<T1,T2>::member_fnc(){
  std::cout << "a = " << a << std::endl;    // or this->a;
  std::cout << "a2 = " << a2 << std::endl;  // or this->a2;                     
  std::cout << "b = " << b << std::endl;
}
IdeaHat
  • 7,641
  • 1
  • 22
  • 53
  • Well, this just moves typedef to class scope. But i would like to do not need writing `base::` or `this` as we do on non template base classes... I think I am asking too much, aint I? – Werner Jan 22 '15 at 15:11
  • @Werner I changed the answer. You still need a little `base::` in the using statements. – IdeaHat Jan 22 '15 at 15:14
  • Ok… it seems that it is the best we can get x) – Werner Jan 22 '15 at 18:38