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++:
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 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_;
};
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:
#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:
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:
$ 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.