簡體   English   中英

C++ 中的模板參數類型分配器

[英]Template parametric type Allocator in C++

我有以下有效的玩具代碼:

#include <cstdlib>
#include <vector>

template <typename T>
struct Dummy {
        using value_type = T;
        using size_type = size_t;
        using difference_type = ptrdiff_t;
        using pointer = value_type*;
        using const_pointer = const value_type*;
        using reference = value_type&;
        using const_reference = const value_type&;

        Dummy() = default;
        Dummy(const Dummy&) = default;
        
        pointer allocate(size_type n, const void* hint = 0) {
                (void)hint;
                return static_cast<pointer>(aligned_alloc(128, n * sizeof(T)));
        }

        void deallocate(void* p, size_type) {
                if (p)
                        free(p);
        }
};

int main()
{
        
        std::vector<int, Dummy<int, 16>> v;
        
        
        for (size_t i = 1; i < 5000; ++i)
                v.push_back(i);
        
        
        return 0;
}

現在我想將 alignment 模板化(而不是放入構造函數中)。 我嘗試在模板中添加一個 integer 參數

template <typename T, size_t align>
struct Dummy {
        using value_type = T;
        using size_type = size_t;
        using difference_type = ptrdiff_t;
        using pointer = value_type*;
        using const_pointer = const value_type*;
        using reference = value_type&;
        using const_reference = const value_type&;

        Dummy() = default;
        Dummy(const Dummy&) = default;

        pointer allocate(size_type n, const void* hint = 0) {
                (void)hint;
                return static_cast<pointer>(aligned_alloc(align, n * sizeof(T)));
        }

        void deallocate(void* p, size_type) {
                if (p)
                        free(p);
        }
};

int main()
{
        
        std::vector<int, Dummy<int, 128>> v;
        
        
        for (size_t i = 1; i < 5000; ++i)
                v.push_back(i);
        
        
        return 0;
}

我在使用g++clang++時遇到很多編譯錯誤:例如

/usr/include/c++/10.2.0/bits/alloc_traits.h:78:11: error: no type named 'type' in 'struct std::__allocator_traits_base::__rebind<Dummy<int, 128>, int, void>'
   78 |     using __alloc_rebind
      |           ^~~~~~~~~~~~~~
In file included from /usr/include/c++/10.2.0/vector:67,
                 from aligned.cpp:4:
/usr/include/c++/10.2.0/bits/stl_vector.h: In instantiation of 'class std::vector<int, Dummy<int, 128> >':
aligned.cpp:62:43:   required from here
/usr/include/c++/10.2.0/bits/stl_vector.h:474:20: error: '_M_allocate' has not been declared in 'std::_Base<int, Dummy<int, 128> >'
  474 |       using _Base::_M_allocate;
      |                    ^~~~~~~~~~~
/usr/include/c++/10.2.0/bits/stl_vector.h:475:20: error: '_M_deallocate' has not been declared in 'std::_Base<int, Dummy<int, 128> >'
  475 |       using _Base::_M_deallocate;

我不明白發生了什么事。 我注意到,即使我不在 class 中使用align ,編譯器也會以同樣的方式抱怨。 相反,如果我在模板中放置了一個未使用的typename ,那么一切都可以正常編譯。 你能幫助我嗎?

我想我找到了解決問題的方法,即使我不確定我是否完全理解所有細節。

感謝: 這個這個我設法寫了

template <typename T, size_t align>
struct Dummy {
        using value_type = T;
        using size_type = size_t;
        using difference_type = ptrdiff_t;
        using pointer = value_type*;
        using const_pointer = const value_type*;
        using reference = value_type&;
        using const_reference = const value_type&;

        template<class Other>
        struct rebind { using other =  Dummy<Other, align>; };



        
        Dummy() {};
        Dummy(const Dummy&) {};

        template <typename Other>
        Dummy(const Dummy<Other, align>&) {}
        
        pointer allocate(size_type n, const void* hint = 0) {
                (void)hint;
                return static_cast<pointer>(aligned_alloc(align, n * sizeof(T)));
        }

        void deallocate(void* p, size_type) {
                if (p)
                        free(p);
        }
};

它編譯並且似乎具有正確的行為。

暫無
暫無

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

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