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.
这篇关于提供给类函数宏调用的参数过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:提供给类函数宏调用的参数过多
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01