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?
Miguel Veganzones is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.