简体   繁体   中英

How do I typedef base class's member template?

I'm trying to make a short form of rebind_alloc as a member template.

The minimal part of my code is as below:

template <class T, class Allocator = std::allocator<T> >
struct A
{
  using allocator_type = Allocator;

  template <class U>
  using rebind_alloc = typename std::allocator_traits<Allocator>::template rebind_alloc<U>;
};

template <class T, class Allocator = std::allocator<T> >
struct B : A<T, Allocator>
{
  using base = A<T>;
  using typename base::allocator_type;

  B(const allocator_type& alloc);
  // B(const base::allocator_type& alloc);

  template <class U>
  using typename base::rebind_alloc<U>; // ERROR ON THIS LINE
};

The reason I wrote every base class's member type is that in the class template inheriting another class template I cannot directly use the member type but use base::some_type form.

A single type like allocator_type is okay, but I got an error when I try to use using statements for the member template.

How do I properly use it?

There are about ten errors in your code. Spot the difference. (Missing semicolons only count as one.)

#include <memory>

template <class T, class Allocator = std::allocator<T> >
struct A
{
  using allocator_type = Allocator;

  template <class U>
  using rebind_alloc =
      typename std::allocator_traits<Allocator>::template rebind_alloc<U>;
};

template <class T, class Allocator = std::allocator<T> >
struct B : A<T, Allocator>
{
  using base = A<T, Allocator>;
  using allocator_type = typename base::allocator_type;

  B(const allocator_type& alloc);

  template <class U>
  using rebind_alloc = typename base::template rebind_alloc<U>;
};

You can declare a new alias template, which uses the base one:

template <class U>
  using rebind_alloc = typename base::rebind_alloc<U>;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM