【问题标题】:alignas/alignof syntax won't compile Visual Studio 2017alignas/alignof 语法无法编译 Visual Studio 2017
【发布时间】:2019-04-13 14:51:30
【问题描述】:

代码部分有效。但如果我改用注释掉的版本,

using StorageType = alignas(alignof(T)) char[sizeof(T)];

我收到错误。

template <typename T> struct minipool {
    union minipool_item {
    private:
        //using StorageType = alignas(alignof(T)) char[sizeof(T)];
        using StorageType = char[sizeof(T)];

        // Points to the next freely available item.
        minipool_item *next;
        // Storage of the item. Note that this is a union
        // so it is shared with the pointer "next" above.
        StorageType datum;
        ....
   };
};

正确的语法是什么?

【问题讨论】:

标签: c++ c++11


【解决方案1】:

它不起作用,因为在 C++ 中,没有机制可以采用现有类型,即char[sizeof(T)],并创建一个除了对齐之外相同的新类型。如果您将datum 声明为与T 对齐的sizeof(T)chars 的数组,则datum 的类型仍然char[sizeof(T)]。对齐规范可以附加到成员声明,​​但不能附加到类型。您不能先将对齐附加到类型,然后使用结果来声明成员,就像您似乎正在尝试做的那样。

using StorageType = char[sizeof(T)];
alignas(T) StorageType datum;

【讨论】:

    猜你喜欢
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    相关资源
    最近更新 更多