簡體   English   中英

如何在其他模板化類內部專門化模板化類?

[英]How to specialize templated class inside other templated class?

我想在其中創建模板化類allocator_factory和模板化arena_allocator arena_allocator繼承自std::allocator我必須為arena_allocator<void>創建arena_allocator<void> ,但我不能。

編譯器錯誤是: arena_alloc.h:25:37: error: too few template-parameter-lists

#pragma once

#include <memory>
#include <cstddef>


template <std::size_t Size, typename Tag>
class allocator_factory;


template <std::size_t Size, typename Tag>
class allocator_factory
{
public:
    static constexpr std::size_t size = Size;
    typedef Tag tag_type;

    template <typename T>
    class arena_allocator;
};



template <std::size_t Size, typename Tag>
class allocator_factory<Size, Tag>::arena_allocator<void> :
    public std::allocator<void>   //^ error here
{
    typedef std::allocator<void> Parent;
public:
    typedef typename Parent::value_type         value_type;
    typedef typename Parent::pointer            pointer;
    typedef typename Parent::const_pointer      const_pointer;
    typedef typename Parent::size_type          size_type;
    typedef typename Parent::difference_type    difference_type;

    typedef allocator_factory<Size,Tag> factory_type;

    template <typename U>
    struct rebind
    {
        typedef typename allocator_factory<size, tag_type>::template arena_allocator<U> other;
    };

    typedef typename Parent::propagate_on_container_move_assignment propagate_on_container_move_assignment;

    arena_allocator() throw() : Parent() {}
    arena_allocator(const arena_allocator& a) throw() : Parent(a) {}
    template <class U>
    arena_allocator(const arena_allocator<U>& a) throw() :Parent(a) {}
};

我認為您不能完全封閉封閉模板而不能封閉封閉模板:

template<class T>
struct A {
    template<class U>
    struct B {};
};

template<>
template<>
struct A<int>::B<int> {}; // Okay.

template<>
template<class U>
struct A<int>::B<U*> {}; // Okay.

template<class T>
template<>
struct A<T>::B<int> {}; // error: enclosing class templates are not explicitly specialized

解決方法是,將隨附的模板提取到文件/命名空間范圍中,並根據需要對其進行專門化:

// Extracted template.
template <std::size_t Size, typename Tag, typename T>
class the_arena_allocator;

template <std::size_t Size, typename Tag>
class allocator_factory
{
public:
    static constexpr std::size_t size = Size;
    typedef Tag tag_type;

    template <typename T>
    using arena_allocator = the_arena_allocator<Size, Tag, T>;
};

// A partial specialization of the extracted template.
template <std::size_t Size, typename Tag>
class the_arena_allocator<Size, Tag, void> { /* ... */ };

暫無
暫無

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

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