Skip to content

[内容勘误] 捉虫:“C++98进阶:类型转换、动态内存与异常处理” 章节的示例代码问题 #96

Description

@HiAirman

相关页面或文件

https://awesome-embedded-learning-studio.github.io/Tutorial_AwesomeModernCPP/vol1-fundamentals/03F-cpp98-casts-memory-exceptions#_2-%E5%8A%A8%E6%80%81%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86

问题类型

代码讲解不准确

当前内容

2.2小结关于placement new的示例代码中有这样一行:

// 预分配的内存缓冲区
alignas(MyClass) uint8_t buffer[sizeof(MyClass)];

问题说明

alignas() 需要传入一个2的整数幂的整型数据,使用 alignof(MyClass) 获取

建议修改

// 预分配的内存缓冲区
alignas(alignof(MyClass)) uint8_t buffer[sizeof(MyClass)];

补充材料

注:本人是C++学习者,这个勘误仅通过搜索引擎和LLM查证

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions