Memory alignment: inconsistency in alignas specifier in templated using declaration

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

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using cacheline_t = alignas(64) char[64];
</code>
<code>using cacheline_t = alignas(64) char[64]; </code>
using cacheline_t = alignas(64) char[64];

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ 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.
</code>
<code>$ 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. </code>
$ 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 used this construct to align a buffer for an arena as such:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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_;
};
</code>
<code>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_; }; </code>
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_;
};

When using fundamental or extended alignments, the buffer does not get properly aligned. Changing the template parameter for a compile time constant fixes the issue. For some reason, any template parameter changes the behavior, it 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.

As a minimum working example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <cstddef>
#include <iostream>
template <std::size_t N>
struct aligned_buffer
{
static constexpr std::size_t alignment = 256; // extended alignment
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';
}
</code>
<code>#include <cstddef> #include <iostream> template <std::size_t N> struct aligned_buffer { static constexpr std::size_t alignment = 256; // extended alignment 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'; } </code>
#include <cstddef>
#include <iostream>

template <std::size_t N>
struct aligned_buffer
{
    static constexpr std::size_t alignment = 256; // extended alignment
    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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Aligned buffer at address 0x7fff59e813f0
Aligned fixed buffer at address 0x7fff59e80f00
1
256
</code>
<code>Aligned buffer at address 0x7fff59e813f0 Aligned fixed buffer at address 0x7fff59e80f00 1 256 </code>
Aligned buffer at address 0x7fff59e813f0
Aligned fixed buffer at address 0x7fff59e80f00
1
256

In this case, I used an extended alignment to show that the buffer is not properly aligned, but the issue holds for fundamental alignments.

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ 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.
</code>
<code>$ 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. </code>
$ 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