假设我们有一个实现std::aligned_storage.我为alignof和alignas运算符定义了两个宏.
#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
}
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,我在问题的标题中得到错误(用Clang编译,与GCC类似的错误).如果我分别用alignof和替换宏,则不会出现错误alignas.为什么是这样?
你开始问我,为什么我这样做之前-原宏有C++ 98兼容的代码,如__alignof和__attribute__((__aligned__(x))),而这些都是具体的编译器,因此宏是我唯一的选择...
编辑: 因此,根据标记为重复的问题,一组额外的括号将解决问题.
std::cout << ALIGNOF((aligned_storage<16, 16>::type)); // compiler error
Run Code Online (Sandbox Code Playgroud)
它没有.那么,我该怎么做呢?(满意的问题?)
我在assert.h中使用断言宏我已经定义了lambda来执行断言检查.
int val1 = 0;
int val2 = 1;
const auto check = [val1,val2]()-> bool
{
return val1 < val2;
};
// no error for this call
assert(check() && "Test is failed");
// no error for this call
assert([=]()-> bool
{
return val1 < val2;
}() && "Test is failed");
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)//compile error for this call "too many arguments provided to function-like macro invocation" assert([val1,val2]()-> bool { return val1 < val2; }() && "Test is failed");
为什么我要来
提供给类似函数的宏调用的参数太多了
当我使用assert宏并在捕获列表中定义带有多个参数的lambda时,编译错误?