带有三个参数的 typedef 是什么意思?

What does a typedef with three arguments mean?

我正在尝试编译包含以下头文件的代码:

#ifndef FFT_H
#define FFT_H

#include<cmath>
#include<complex.h>
#include<vector>

#define Vec(a, b) std::vector<__typeof(*(a))> ((a), (a)+(b))

typedef double complex complex_t; // this is my trouble line!
typedef double real_t;

#endif

我的编译器因 typedef double complex complex_t;

而出错

这对我来说很有意义,因为大多数类型定义只包含两个参数:类型和别名。

此已发布的代码来自 reputable source,因此我认为我做错了什么。但是,我对为什么 typedef 会使用 3 个参数感到困惑。

是的,大多数 typedef 都包含别名和基础类型,但是没有要求类型是单个标记:

typedef unsigned long long int ULLI;
//      \____________________/
//  Many tokens for underlying type

因此,无论您的代码在何种情况下运行,似乎都已经具有 double complex 类型。

此类型 实际上 C 标准的一部分,在 <complex.h> 中保留,但等效的 C++ <ccomplex>/<complex.h> header 现在已被替换使用 <complex>,更适合 C++,因此包含它并使用 complex<double> 类型会更合适。

我相信,从 C++17 开始,<ccomplex>/<complex.h> 不再持有任何遗留的 C 东西,而是开始包括来自 C++ 标准库的其他 non-legacy header。

然后,在 C++20 中,他们完全放弃了它。来自(略有释义)C++20 [diff.cpp17.library],其中详细说明了差异:

Change: Remove vacuous C++ header files.

Rationale: Empty headers implied a false requirement to achieve C compatibility with the C++ headers.

Effect on original feature: A valid C++ 2017 program that performs a #include of <ccomplex> (amongst others) may fail to compile. To retain the same behavior, a #include of <ccomplex> can be replaced by a #include of <complex>.

complex 不是 C++ 中的关键字,double complex 不是类型。你把 C++ 和另一种语言混在一起了。

您可以使用 std::complex<double>,带或不带 typedef。

它很老套,但将 更改为“/usr/include/complex.h”,这将强制作者打算包含。然后它应该建立。至少它对我适用于 fedora 33 和 g++ 版本:

> g++ --version 
g++ (GCC) 10.2.1 20201125 (Red Hat 10.2.1-9) Copyright (C) 2020 Free Software 
Foundation, Inc. This is free software; see the source for copying conditions.  
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR 
PURPOSE.