Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a typedef with three arguments mean?

I'm trying to compile code that includes the following header file:

#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

My compiler is tripping up over typedef double complex complex_t;

This makes sense to me because most typedefs include only two arguments: the type, and the alias name.

This published code is from a reputable source so I assume I'm doing something wrong on my end. However, I'm confused as to why a typedef would ever use 3 arguments.

like image 413
Izzo Avatar asked Apr 28 '20 00:04

Izzo


2 Answers

Yes, most typedefs contain the alias name and the underlying type, but there's no requirement that the type be a single token:

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

So whatever your code works under appears to have already a double complex type.

This type is actually part of the C standard, held in <complex.h> but the equivalent C++ <ccomplex>/<complex.h> header has now been replaced with <complex>, more suited to C++, so it would be more proper to include that and use the complex<double> type.

I believe, as of C++17, <ccomplex>/<complex.h> stopped holding any legacy C stuff and just started including other non-legacy headers from the C++ standard library.

And then, in C++20, they ditched it altogether. From (slightly paraphrased) C++20 [diff.cpp17.library], which details the differences:

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

like image 98
paxdiablo Avatar answered Sep 22 '22 04:09

paxdiablo


complex is not a keyword in C++, and double complex is not a type . You are mixing up C++ with another language .

Instead you can use std::complex<double>, with or without a typedef.

like image 43
M.M Avatar answered Sep 24 '22 04:09

M.M