Say we have an implementation of std::aligned_storage
. I've defined two macros for the alignof
and alignas
operators.
#include <iostream>
#include <cstddef>
#define ALIGNOF(x) alignof(x)
#define ALIGNAS(x) alignas(x)
template<std::size_t N, std::size_t Al = ALIGNOF(std::max_align_t)>
struct aligned_storage
{
struct type {
ALIGNAS(Al) unsigned char data[N];
};
};
int main()
{
// first case
std::cout << ALIGNOF(aligned_storage<16>::type); // Works fine
// second case
std::cout << ALIGNOF(aligned_storage<16, 16>::type); // compiler error
}
In the second case I get the error in the title of the question (compiling with Clang, similar error with GCC). The error is not present if I replace the macros with alignof
and alignas
respectively. Why is this?
Before you start asking me why I'm doing this - the original macros have C++98 compatible code such as __alignof
and __attribute__((__aligned__(x)))
and those are compiler specific, so macros are my only choice...
EDIT: So according to the question marked as duplicate, an extra set of parenthesis would fix the issue.
std::cout << ALIGNOF((aligned_storage<16, 16>::type)); // compiler error
It doesn't. So, how would I go about doing this? (Satisfiable question?)