提供给类似函数的宏调用的参数过多 [英] Too many arguments provided to function-like macro invocation

查看:142
本文介绍了提供给类似函数的宏调用的参数过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

在第二种情况下,我得到问题标题的错误(使用Clang进行编译,使用GCC时发生类似的错误).如果我分别用 alignof alignas 替换宏,则不会出现该错误.这是为什么?

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?

在开始问我为什么要这样做之前-原始宏具有与C ++ 98兼容的代码,例如 __ alignof __ attribute __((__ aligned __(x)))并且它们是特定于编译器的,因此宏是我唯一的选择...

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...

因此,根据标记为重复的问题,再加上一组括号即可解决该问题.

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 ++预处理器不知道任何C/C ++语言构造,它只是具有自己的语法和规则的文本预处理器.根据该语法,以下代码 ALIGNOF(aligned_storage< 16,16> :: type)是具有2个参数( aligned_storage< 16 16> :: type ),因为括号内有逗号.

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.

我建议您 typedef aligned_storage< 16,16> 并在此宏调用中使用该类型.

I would suggest you to typedef aligned_storage<16, 16> and use that type inside this macro invocation.

这篇关于提供给类似函数的宏调用的参数过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆