Too many arguments provided to function-like macro invocation(提供给类函数宏调用的参数过多)
问题描述
假设我们有一个 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 个参数调用宏 ALIGNOF
(aligned_storage<16
code> 和 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.
这篇关于提供给类函数宏调用的参数过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:提供给类函数宏调用的参数过多


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01