相关疑难解决方法(0)

提供给类似函数的宏调用的参数太多

假设我们有一个实现std::aligned_storage.我为alignofalignas运算符定义了两个宏.

#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)

它没有.那么,我该怎么做呢?(满意的问题?)

c++ macros

15
推荐指数
2
解决办法
9095
查看次数

在Xcode中定义lambda(assert.h)中的lambda时,为类似函数的宏调用编译错误提供了太多参数[c ++]

我在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)
//compile error for this call "too many arguments provided to function-like macro invocation"
assert([val1,val2]()-> bool
       {
           return val1 < val2;
       }() && "Test is failed");
Run Code Online (Sandbox Code Playgroud)

为什么我要来

提供给类似函数的宏调用的参数太多了

当我使用assert宏并在捕获列表中定义带有多个参数的lambda时,编译错误?

c++ macros lambda xcode c++11

7
推荐指数
1
解决办法
6027
查看次数

标签 统计

c++ ×2

macros ×2

c++11 ×1

lambda ×1

xcode ×1