Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在其他模板类中专门化模板类?_C++_Templates_Template Specialization - Fatal编程技术网

C++ 如何在其他模板类中专门化模板类?

C++ 如何在其他模板类中专门化模板类?,c++,templates,template-specialization,C++,Templates,Template Specialization,我想在其中创建模板化类分配器工厂和模板化竞技场分配器。就arena\u分配器继承自std::allocator而言,我必须为arena\u分配器创建专门化,但我不能 编译器错误为:arena\u alloc.h:25:37:错误:模板参数列表太少 #pragma once #include <memory> #include <cstddef> template <std::size_t Size, typename Tag> class allocat

我想在其中创建模板化类
分配器工厂
和模板化
竞技场分配器
。就
arena\u分配器
继承自
std::allocator
而言,我必须为
arena\u分配器
创建专门化,但我不能

编译器错误为:
arena\u alloc.h:25:37:错误:模板参数列表太少

#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) {}
};
#pragma一次
#包括
#包括
模板
类分配器工厂;
模板
类分配器工厂
{
公众:
静态constexpr std::size\u t size=size;
类型定义标签类型;
模板
类分配程序;
};
模板
类分配器\工厂::竞技场\分配器:
公共std::分配器//^此处有错误
{
typedef std::分配器父级;
公众:
typedef typename父项::值\类型值\类型;
typedef typename父级::指针指针;
typedef typename父项::常量指针常量指针;
typedef typename父项::大小\类型大小\类型;
typedef typename父项::差异类型差异类型;
typedef分配器\u工厂\u类型;
模板
结构重新绑定
{
typedef typename分配器_工厂::模板arena_分配器其他;
};
typedef typename父项::在容器上传播\u移动\u分配在容器上传播\u移动\u分配;
arena_分配器()throw():Parent(){}
竞技场分配器(const竞技场分配器&a)throw():父(a){}
模板
竞技场分配器(const竞技场分配器&a)throw():父(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
模板
结构A{
模板
结构B{};
};
模板
模板
结构A::B{};//可以
模板
模板
结构A::B{};//可以
模板
模板
结构A::B{};//错误:封闭类模板未显式专用

作为一种解决方法,请将附带的模板提取到文件/命名空间范围中,并根据需要对其进行专门化:

// 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> { /* ... */ };
//提取的模板。
模板
对分配程序进行分类;
模板
类分配器工厂
{
公众:
静态constexpr std::size\u t size=size;
类型定义标签类型;
模板
使用竞技场分配器=竞技场分配器;
};
//提取模板的部分专门化。
模板
为分配程序{/*…*/}初始化;

我所有使用封闭模板的移动都是将分配器作为一个参数模板离开,并将大小和标记隐藏在里面it@user2807083您的成员模板类仍然由封闭的模板参数参数参数化。区别只是句法上的。