C++标准模板(STL)- 类型支持 (杂项变换,定义适于用作所有给定类型的未初始化存储的类型,std::aligned_union)

类型特性

类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。

试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。

定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。

杂项变换

定义适于用作所有给定类型的未初始化存储的类型

复制代码
std::aligned_union

|---------------------------------------------------------------------|---|-----------|
| template< std::size_t Len, class... Types > struct aligned_union; | | (C++11 起) |

提供嵌套类型 type ,它是平凡的标准布局类型,且其大小和对齐适合用作任何列于 Types 的类型的一个对象的未初始化存储。存储的大小至少为 Lenstd::aligned_union 亦确定所有 Types 中最严格(最大)的对齐要求,使之可用作常量 alignment_value

若 sizeof...(Types) == 0 ,则行为未定义。

是否支持任何扩展对齐是实现定义的。

成员类型

|--------|----------------------------|
| 名称 | 定义 |
| type | 适用于存储来自 Types 的任何类型的平凡类型 |

辅助类型

|---------------------------------------------------------------------------------------------------------------------|---|-----------|
| template< std::size_t Len, class... Types > using aligned_union_t = typename aligned_union<Len,Types...>::type; | | (C++14 起) |

成员常量

|------------------------|------------------------------|
| alignment_value [静态] | 所有 Types 的最严格对齐 (公开静态成员常量) |

可能的实现

复制代码
#include <algorithm>
template <std::size_t Len, class... Types>
struct aligned_union
{
    static constexpr std::size_t alignment_value = std::max({alignof(Types)...});
 
    struct type
    {
      alignas(alignment_value) char _s[std::max({Len, sizeof(Types)...})];
    };
};

调用示例

复制代码
#include <iostream>
#include <type_traits>
#include <string>

template<class T, std::size_t N>
class static_vector
{
    // N 个 T 的正确对齐的未初始化存储
    typename std::aligned_union<sizeof(T), T>::type data[N];
    std::size_t m_size = 0;

public:
    // 于对齐存储创建对象
    template<typename ...Args> void emplace_back(Args&&... args)
    {
        if (m_size >= N)  // 可行的错误处理
            throw std::bad_alloc{};
        new (data + m_size) T(std::forward<Args>(args)...);
        ++m_size;
    }

    // 访问对齐存储中的对象
    const T& operator[](std::size_t pos) const
    {
        // 注意: C++17 起需要 std::launder
        return *reinterpret_cast<const T*>(data + pos);
    }

    // 从对齐存储删除对象
    ~static_vector()
    {
        for (std::size_t pos = 0; pos < m_size; ++pos)
        {
            // 注意: C++17 起需要 std::launder
            reinterpret_cast<T*>(data + pos)->~T();
        }
    }
};

int main()
{
    static_vector<std::string, 10> v1;
    v1.emplace_back(5, '*');
    v1.emplace_back(10, '*');
    std::cout << v1[0] << std::endl << v1[1] << std::endl;
    return 0;
}

输出

复制代码
*****
**********
相关推荐
bikong76 分钟前
桥接模式,打造灵活可扩展的日志系统C++
c++·桥接模式
艾莉丝努力练剑10 分钟前
【C++】类和对象(下):初始化列表、类型转换、Static、友元、内部类、匿名对象/有名对象、优化
linux·运维·c++·经验分享
疋瓞24 分钟前
C++_STL和数据结构《1》_STL、STL_迭代器、c++中的模版、STL_vecto、列表初始化、三个算法、链表
数据结构·c++·算法
三体世界35 分钟前
测试用例全解析:从入门到精通(1)
linux·c语言·c++·python·功能测试·测试用例·测试覆盖率
Bear on Toilet1 小时前
继承类模板:函数未在模板定义上下文中声明,只能通过实例化上下文中参数相关的查找找到
开发语言·javascript·c++·算法·继承
努力努力再努力wz2 小时前
【c++进阶系列】:map和set的模拟实现(附模拟实现的源码)
java·linux·运维·开发语言·c++
lingran__5 小时前
速通ACM省铜第三天 赋源码(Double Perspective和Trip Shopping和Hamiiid, Haaamid... Hamid?)
c++·算法
凤城老人5 小时前
C++使用拉玛努金公式计算π的值
开发语言·c++·算法
YaoYuan93237 小时前
C++ 类型推导(第一部分)
c++
夜猫逐梦8 小时前
【VC】 error MSB8041: 此项目需要 MFC 库
c++·mfc