簡體   English   中英

如何鍵入def基類的成員模板?

[英]How do I typedef base class's member template?

我正在嘗試將rebind_alloc為成員模板。

我的代碼的最小部分如下所示:

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
};

我寫每個基類的成員類型的原因是,在繼承另一個類模板的類模板中,我不能直接使用成員類型,而只能使用base::some_type形式。

allocator_type這樣的單一類型是可以的,但是當我嘗試對成員模板使用using語句時出現錯誤。

如何正確使用它?

您的代碼中大約有十個錯誤。 指出不同。 (缺少分號只能算作一個。)

#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>;
};

您可以聲明一個新的別名模板,該模板使用基本模板:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM