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?)
C/C++ preprocessor is not aware of any C/C++ language constructs, it is just text preprocessor with its own syntax and rules. According to that syntax the following code ALIGNOF(aligned_storage<16, 16>::type) is invocation of macro ALIGNOF with 2 arguments (aligned_storage<16 and 16>::type) because there is comma inside parentheses.
I would suggest you to typedef aligned_storage<16, 16> and use that type inside this macro invocation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With