_Complex long int

不羁的心 提交于 2019-12-13 10:33:53

问题


tl;dr;

Q what does int mean in _Complex long int? Why is it legal?

Longer version

I was porting some 32-bit code to be 64-bit safe. In one place I noted it uses:

static __complex__ long int i32X[256];

A dumb search and replace changing "long int" for int32_t gave me a compilation error. __complex__ is an older GNUism which has been replaced by the standard _Complex. Here is a short code snippet to reproduce the issue:

#include <complex.h>
#include <inttypes.h>

typedef _Complex long int WhyAmILegal;

typedef _Complex int32_t Snafu;

typedef int32_t _Complex Snafu2;

Compiled under gcc gives:

complextest.c:6:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’     before ‘Snafu’
typedef _Complex int32_t Snafu;
                      ^~~~~
complextest.c:8:17: error: two or more data types in declaration specifiers
typedef int32_t _Complex Snafu2;

The (draft) C11 standard says:

"There are three complex types, designated as float _Complex, double _Complex, and long double _Complex.43) (Complex types are a conditional feature that implementations need not support; see 6.10.8.3.) The real floating and complex types are collectively called the floating types."

So the bug here would seem to be that I am trying to request an integral complex type when only floating point complex types are legal. The meaning of _Complex long is actually more like _Complex long double. I thought it might be a bug in gcc's parser, however

_Complex long int

compiles just fine under clang online as well.

So why is int 'legal' here.

Thanks to @Keith-Thompson's for pointing out that integral complex numbers are a GNU extension and one that is supported by clang.

Another data point. The following program:

#include <complex.h>
#include <inttypes.h>
#include <stdio.h>

typedef _Complex long int ComplexLong;
typedef _Complex short int ComplexShort;
typedef _Complex int ComplexInt;

int main(void)
{
  fprintf(stderr,"sizeof(_Complex long int)=%d\n",sizeof(ComplexLong));
  fprintf(stderr,"sizeof(_Complex int)=%d\n",sizeof(ComplexInt));
  fprintf(stderr,"sizeof(_Complex short int)=%d\n",sizeof(ComplexShort));
  fprintf(stderr,"sizeof(short int)=%d\n",sizeof(short int));
  fprintf(stderr,"sizeof(int)=%d\n",sizeof(int));
  fprintf(stderr,"sizeof(long int)=%d\n",sizeof(long int));
  return 0;
}

Compiled using:

all: complextest complextest32

complextest: complextest.c
    $(CC) -o$@ $<

complextest32: complextest.c
    $(CC) -m32 -o$@ $<

when (compiled using gcc) and run gives:

>./complextest
sizeof(_Complex long int)=16
sizeof(_Complex int)=8
sizeof(_Complex short int)=4
sizeof(short int)=2
sizeof(int)=4
sizeof(long int)=8
>./complextest32
sizeof(_Complex long int)=8
sizeof(_Complex int)=8
sizeof(_Complex short int)=4
sizeof(short int)=2
sizeof(int)=4
sizeof(long int)=4

So _Complex long int like long cannot be specified in an architecture invariant manner. You should use _Complex int or _Complex short to be slightly more portable.


回答1:


It's a gcc extension, as you can see by compiling in (more or less) conforming C mode:

$ cat c.c
_Complex double cd;
_Complex long int cli;
$ gcc -c c.c
$ gcc -c -std=c11 -pedantic c.c
c.c:2:1: warning: ISO C does not support complex integer types [-Wpedantic]
 _Complex long int cli;
 ^~~~~~~~
$

This is documented in the gcc manual:

ISO C99 supports complex floating data types, and as an extension GCC supports them in C90 mode and in C++. GCC also supports complex integer data types which are not part of ISO C99.

clang, not surprisingly, supports the same extension.

To answer the added question, _Complex is a type specifier. _Complex int32_t is invalid for the same reason unsigned int32_t is invalid. Type specifiers can't be applied to typedefs.



来源:https://stackoverflow.com/questions/46395458/complex-long-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!