c++ - 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;
}
现在我想对对齐进行模板化(而不是放入构造函数)。我尝试在模板中添加一个整数参数
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;
我不明白发生了什么事。我注意到即使我不在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);
}
};
它编译并且似乎具有正确的行为。
推荐阅读
- javascript - 如何对 jQuery 对象进行故障排除?
- bash - 在 shell 脚本中使用 TeamCity 环境变量
- amazon-web-services - Kubernetes - Ingress-nginx 路由错误(无法将前端连接到后端)
- android - Android偏好默认值重复资源
- java - 将动态列值绑定到对象
- javascript - 使用javascript更改值时,使文本不通过元素
- java - Spring 安全用户主体不断以匿名用户身份返回
- javascript - 为什么找不到 db0bjects
- java - 使用 ConcurrentHashMap 更新 NullPointerException 问题的代码
- javascript - 我想通过 Fetch/Axios 中的 formData 发送包含图像的字典对象数组