Memory alignment: inconsistency in extended alignment with alignas specifier in using declaration

I have encountered what I believe to be an inconsistency using the alignas specifier in using declarations. Where any template messes up the alignment.

Based on the example provided in this cppreference page, I understand the following is valid c++:

using cacheline_t = alignas(64) char[64];

Clang does not compile the code, but gcc does support it.

$ clang++ test.cpp -Wall -Wextra -Wpedantic -std=c++23 -o out.o
test.cpp:34:21: error: an attribute list cannot appear here
   34 | using cacheline_t = alignas(64) char[64];
      |                     ^~~~~~~~~~~
1 error generated.

I have successfully used this construct to align a buffer for an arena as such:

template <std::size_t N>
struct aligned_buffer
{
    static constexpr std::size_t alignment = alignof(std::max_align_t);
    using buffer_t                         = alignas(alignment) std::byte[N];

    buffer_t buffer_;
};

But, when using extended alignments, the buffer is does not get properly aligned. Changing the template parameter for a compile time constant fixes the issue. For some reason, the template parameter changes the behavior, and can be other parameter rather than the size. Avoiding the using declaration also removes the inconsistency. Tested in both gcc13.3.0 and gcc 14.1.1.

From Embracing Modern C++ Safely, section 2.1 alignas:

In contrast, whether any extended alignment is supported at all and, if so,
in which contexts, is implementation defined.1 For example, the strictest
supported extended alignment for a variable with static storage duration
might be as large as 228 or 229 or as small as 213.

I thus assume that an extended alignment of 256 is supported by gcc. As eliding the using declaration and declaring the buffer as:

alignas(alignment) std::byte buffer_[N];

results in the expected boundary alignment too, I conclude that the issue is in the templated using declaration.

As a minimum working example:

#include <cstddef>
#include <iostream>

template <std::size_t N>
struct aligned_buffer
{
    static constexpr std::size_t alignment = 256;
    using buffer_t                         = alignas(alignment) std::byte[N];

    aligned_buffer()
    {
        std::cout << "Aligned buffer at address " << std::begin(buffer_) << 'n';
    }

    buffer_t buffer_;
};

struct aligned_fixed_buffer
{
    static constexpr std::size_t N         = 1024;
    static constexpr std::size_t alignment = 256;
    using buffer_t                         = alignas(alignment) std::byte[N];

    aligned_fixed_buffer()
    {
        std::cout << "Aligned fixed buffer at address " << std::begin(buffer_) << 'n';
    }

    buffer_t buffer_;
};

int main()
{
    aligned_buffer<1024> buffer1;
    aligned_fixed_buffer buffer2;
    std::cout << alignof(decltype(buffer1)) << 'n';
    std::cout << alignof(decltype(buffer2)) << 'n';
}

Which provides a possible output of:

Aligned buffer at address 0x7fff59e813f0
Aligned fixed buffer at address 0x7fff59e80f00
1
256

The templated version is not properly aligned, while the compile time constant is. Both the template and the using declaration are needed to mess up the alignment. This behavior is consistent in both gcc 13.3.0 and gcc 14.1.1 using the c++23 standard.

While Clang 17.0.6 does not compile the code yielding these errors:

$ clang++ test.cpp -Wall -Wextra -Wpedantic -std=c++23 -o out.o
test.cpp:9:46: error: 'alignas' attribute cannot be applied to types
    9 |     using buffer_t                         = alignas(alignment) std::byte[N];
      |                                              ^
test.cpp:23:46: error: 'alignas' attribute cannot be applied to types
   23 |     using buffer_t                         = alignas(alignment) std::byte[N];
      |                                              ^
2 errors generated.

Is this behavior expected? What could cause this inconsistency?

New contributor

Miguel Veganzones is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật