C++ 自定义分配器大小参数作为模板参数会引发编译器错误

标签 c++

当我为容器使用自定义分配器时,以下代码给出了预期的结果(将大小 sz 保留为全局变量)

#include <cstddef> /* size_t */
#include <new> /* Only for bad_alloc */
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std::endl;
std::size_t constexpr sz = 4;
template <typename T> class StaticAllocator {
protected:
    static T buf[sz];
public:
    typedef T value_type;
    T* allocate(std::size_t const n) {
        if (n > sz) throw std::bad_alloc();
        return buf;
    }
    void deallocate(T*, std::size_t) {}
};
template<typename T> T StaticAllocator<T>::buf[sz];
int main(void) {
    std::vector<char, StaticAllocator<char> > v;
    v.push_back('a');
    v.push_back('b');
    for (auto const& i : v) cout << i << endl;
}

当我尝试使用大小作为类的模板参数时,此版本的代码会出现编译器错误

#include <cstddef> /* size_t */
#include <new> /* bad_alloc */
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std::endl;
template<typename T, std::size_t sz> class StaticAllocator {
protected:
    static T buf[sz];
public:
    typedef T value_type;
    T* allocate(std::size_t const n) {
        if (n > sz) throw std::bad_alloc();
        return buf;
    }
    void deallocate(T*, std::size_t) {}
};
template<typename T, std::size_t sz> T StaticAllocator<T, sz>::buf[sz];
int main(void) {
    std::vector<char, StaticAllocator<char, 4> > v;
    v.push_back('a');
    v.push_back('b');
    for (auto const& i : v) cout << i << endl;
}

最佳答案

获取某种类型的分配器U来自类型 T 的分配器,成员别名模板 std::allocator_traits::rebind_alloc<U> 使用[allocator.traits.types]:

Alloc::rebind<T>::other if Alloc::rebind<T>::other is valid and denotes a type; otherwise, Alloc<T, Args> if Alloc is a class template instantiation of the form Alloc<U, Args>, where Args is zero or more type arguments; otherwise, the instantiation of rebind_alloc is ill-formed.

请注意Args类型模板参数。在你的分配器中没有 rebind 。第一种情况Alloc<U>使用,Args是空的。但在第二种情况下,第二个模板参数是一个非类型参数,它无法与 Args 匹配。 .

您需要手动添加rebind成员结构:

template<typename T, std::size_t sz>
class StaticAllocator {
    // ...

    template<class U>
    struct rebind {
        using other = StaticAllocator<U, sz>;
    };
};

另请注意,您的分配器对于某些通用类型 S 已损坏。第一,buf将默认构造 sz 进行初始化对象 S 。然后,它将被新构造的 S 覆盖。在销毁现有位置之前先将其放在同一位置。重新分配时也会发生类似的情况。这可能会导致未定义的行为。请参阅thisthis询问一些细节。

<小时/>

现已删除的答案中提出了以下解决方案:继承自 std::allocator<T> :

template<typename T, std::size_t sz> class StaticAllocator : 
    public std::allocator<T> {
// ...
};

代码编译并运行,但是... StaticAllocator::allocateStaticAllocator::deallocate不被调用(至少在 libstdc++ 中)。原因是内部std::vector总是uses rebind 获取分配器类型:

using Tp_alloc_type = 
    typename gnu_cxx::alloc_traits<Alloc>::template rebind<Tp>::other;

rebind继承自 std::allocator<T>它返回 std::allocator而不是StaticAllocator 。这就是为什么您仍然需要提供自己的 rebindStaticAllocator .

关于C++ 自定义分配器大小参数作为模板参数会引发编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59597432/

相关文章:

c++ - 需要一种在 C++ 中设置所有接口(interface)上的 DNS 服务器的方法

c++ - 除了 increment 语句外,如何制作 for 循环变量 const?

c++ - 带有模板的奇怪解析行为 _Atomic

c++ - clang mac 链接错误与 gtest 体系结构 x86_64 的 undefined symbol

c++ - 错误 : more than one instance of overloaded function matches argument list

c++ - 递归可迭代模板函数 C++

c++ - 如何使用 Visual Studio 2013 构建 Boost

c++ - 返回 const 指针(例如 int * const)的函数的用例是什么?

c++ - 如何在 lambda 中捕获此对象的变量?

c++ - 将 -lm 标志传递给 qt 编译器以摆脱 "cannot open file “m.lib”