我想创建模板化类allocator_factory
并在其中创建模板化arena_allocator
。就arena_allocator
继承自std::allocator
而言,我必须为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) {}
};
答案 0 :(得分:1)
我认为您不能完全封闭封闭模板,而不能封闭模板:
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> { /* ... */ };