Re: sizeof

Russell Shaw
2022-05-09T07:22:13Z

On 9/5/22 3:29 am, Simon Marchi wrote:
>
>
> On 2022-05-08 11:22, Russell Shaw wrote:
>> When inspecting a C++ file, i get:
>>
>> (gdb) p sizeof(int())
>> $82 = 1
>>
>> (gdb) p sizeof(int)
>> $83 = 4
>>
>> Not right ?
>>
>> Package: gdb
>> Version: 11.2-1
>> on debian
>
> The compiler seems to agree with GDB:
>
> $ cat test.cpp
> #include <stdio.h>
>
> int main()
> {
> printf("%zu\n", sizeof(int));
> printf("%zu\n", sizeof(int()));
> }
> $ g++ test.cpp
> test.cpp: In function ‘int main()’:
> test.cpp:6:19: warning: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]
> 6 | printf("%zu\n", sizeof(int()));
> | ^~~~~~~~~~~~~
> $ ./a.out
> 4
> 1
>
> I don't really know what sizeof(int()) means anyway. clang just rejects
> it:
>
> $ clang++ test.cpp
> test.cpp:6:19: error: invalid application of 'sizeof' to a function type
> printf("%zu\n", sizeof(int()));
> ^ ~~~~~~~

sizeof(int()) should transform to sizeof(int (*)()) according to the
C++20 standard.

**********************************************************************
cat main.cpp

void f(int()) { }

int a = f;

int main() { }

g++ -DHAVE_CONFIG_H -I. -std=c++20 -Wall -Wno-unused -g -O0 -MT
main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
main.cpp:8:9: error: invalid conversion from ‘void (*)(int (*)())’ to
‘int’ [-fpermissive]
8 | int a = f;
| ^
| |
| void (*)(int (*)())

**********************************************************************