public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* sizeof
@ 2022-05-08 15:22 Russell Shaw
  2022-05-08 17:29 ` sizeof Simon Marchi
  0 siblings, 1 reply; 15+ messages in thread
From: Russell Shaw @ 2022-05-08 15:22 UTC (permalink / raw)
  To: GDB mailing list

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-08 15:22 sizeof Russell Shaw
@ 2022-05-08 17:29 ` Simon Marchi
  2022-05-08 17:48   ` sizeof Andreas Schwab
  2022-05-09  7:21   ` sizeof Russell Shaw
  0 siblings, 2 replies; 15+ messages in thread
From: Simon Marchi @ 2022-05-08 17:29 UTC (permalink / raw)
  To: Russell Shaw, GDB mailing list



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()));
                      ^     ~~~~~~~

Simon

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-08 17:29 ` sizeof Simon Marchi
@ 2022-05-08 17:48   ` Andreas Schwab
  2022-05-09  7:21   ` sizeof Russell Shaw
  1 sibling, 0 replies; 15+ messages in thread
From: Andreas Schwab @ 2022-05-08 17:48 UTC (permalink / raw)
  To: Simon Marchi via Gdb; +Cc: Russell Shaw, Simon Marchi

On Mai 08 2022, Simon Marchi via Gdb wrote:

> I don't really know what sizeof(int()) means anyway.

It's a GNU extension, much like sizeof(void), so that you can subtract
function pointers.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-08 17:29 ` sizeof Simon Marchi
  2022-05-08 17:48   ` sizeof Andreas Schwab
@ 2022-05-09  7:21   ` Russell Shaw
  2022-05-09  7:35     ` sizeof Andreas Schwab
  2022-05-10  3:46     ` sizeof Russell Shaw
  1 sibling, 2 replies; 15+ messages in thread
From: Russell Shaw @ 2022-05-09  7:21 UTC (permalink / raw)
  To: GDB mailing list

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 (*)())

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-09  7:21   ` sizeof Russell Shaw
@ 2022-05-09  7:35     ` Andreas Schwab
  2022-05-10  3:46     ` sizeof Russell Shaw
  1 sibling, 0 replies; 15+ messages in thread
From: Andreas Schwab @ 2022-05-09  7:35 UTC (permalink / raw)
  To: Russell Shaw; +Cc: GDB mailing list

On Mai 09 2022, Russell Shaw wrote:

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

No, see [expr.sizeof] #3.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-09  7:21   ` sizeof Russell Shaw
  2022-05-09  7:35     ` sizeof Andreas Schwab
@ 2022-05-10  3:46     ` Russell Shaw
  2022-05-10  7:22       ` sizeof Andreas Schwab
  1 sibling, 1 reply; 15+ messages in thread
From: Russell Shaw @ 2022-05-10  3:46 UTC (permalink / raw)
  To: gdb

 >Andreas Schwab wrote:
 >On Mai 09 2022, Russell Shaw wrote:
 >
 >> sizeof(int()) should transform to sizeof(int (*)()) according to the C++20
 >> standard.
 >
 >No, see [expr.sizeof] #3.

What does "[expr.sizeof] #3" refer to ?

unary-expression :
   ...
   sizeof ( type-id )

type-id :
   type-specifier-seq abstract-declarator(opt)


No expressions here.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-10  3:46     ` sizeof Russell Shaw
@ 2022-05-10  7:22       ` Andreas Schwab
  2022-05-10 14:20         ` sizeof Russell Shaw
  0 siblings, 1 reply; 15+ messages in thread
From: Andreas Schwab @ 2022-05-10  7:22 UTC (permalink / raw)
  To: Russell Shaw; +Cc: gdb

On Mai 10 2022, Russell Shaw wrote:

>>Andreas Schwab wrote:
>>On Mai 09 2022, Russell Shaw wrote:
>>
>>> sizeof(int()) should transform to sizeof(int (*)()) according to the C++20
>>> standard.
>>
>>No, see [expr.sizeof] #3.
>
> What does "[expr.sizeof] #3" refer to ?

Paragrph #3 in section [expr.sizeof].

    "The lvalue-to-rvalue (7.3.1), array-to-pointer (7.3.2), and
    function-to-pointer (7.3.3) standard conversions are not applied to
    the operand of sizeof."

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-10  7:22       ` sizeof Andreas Schwab
@ 2022-05-10 14:20         ` Russell Shaw
  2022-05-10 14:48           ` sizeof Andreas Schwab
                             ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Russell Shaw @ 2022-05-10 14:20 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: gdb

On 10/5/22 5:22 pm, Andreas Schwab wrote:
> On Mai 10 2022, Russell Shaw wrote:
> 
>>> Andreas Schwab wrote:
>>> On Mai 09 2022, Russell Shaw wrote:
>>>
>>>> sizeof(int()) should transform to sizeof(int (*)()) according to the C++20
>>>> standard.
>>>
>>> No, see [expr.sizeof] #3.
>>
>> What does "[expr.sizeof] #3" refer to ?
> 
> Paragrph #3 in section [expr.sizeof].
> 
>      "The lvalue-to-rvalue (7.3.1), array-to-pointer (7.3.2), and
>      function-to-pointer (7.3.3) standard conversions are not applied to
>      the operand of sizeof."

"Standard conversions" are only applied to expressions, not type-id (which is an 
abstract declarator syntax).

7.6.2.5 Sizeof [expr.sizeof]
3) only applies to the first case "sizeof unary-expression", not the second 
"sizeof ( type-id )".

unary-expression :
   ...
   sizeof unary-expression
   sizeof ( type-id )


**************
7.6.2.5 Sizeof [expr.sizeof]
3)
  The lvalue-to-rvalue (7.3.2), array-to-pointer (7.3.3), and 
function-to-pointer (7.3.4) standard conversions are not applied to the operand 
of sizeof. If the operand is a prvalue, the temporary materialization conversion 
(7.3.5) is applied.

**************
7.3 Standard conversions [conv]
7.3.1 General [conv.general]

A standard conversion sequence will be applied to an expression if necessary to 
convert it to a required destination type.
**************

I don't seem to get all replies on the gdb list so i'm including your email in 
case you don't get all replies either. I have to check the gdb web page archive.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-10 14:20         ` sizeof Russell Shaw
@ 2022-05-10 14:48           ` Andreas Schwab
  2022-05-10 15:43           ` sizeof Russell Shaw
  2022-05-10 15:48           ` sizeof Russell Shaw
  2 siblings, 0 replies; 15+ messages in thread
From: Andreas Schwab @ 2022-05-10 14:48 UTC (permalink / raw)
  To: Russell Shaw; +Cc: gdb

On Mai 11 2022, Russell Shaw wrote:

> On 10/5/22 5:22 pm, Andreas Schwab wrote:
>> On Mai 10 2022, Russell Shaw wrote:
>> 
>>>> Andreas Schwab wrote:
>>>> On Mai 09 2022, Russell Shaw wrote:
>>>>
>>>>> sizeof(int()) should transform to sizeof(int (*)()) according to the C++20
>>>>> standard.
>>>>
>>>> No, see [expr.sizeof] #3.
>>>
>>> What does "[expr.sizeof] #3" refer to ?
>> Paragrph #3 in section [expr.sizeof].
>>      "The lvalue-to-rvalue (7.3.1), array-to-pointer (7.3.2), and
>>      function-to-pointer (7.3.3) standard conversions are not applied to
>>      the operand of sizeof."
>
> "Standard conversions" are only applied to expressions, not type-id (which
> is an abstract declarator syntax).

So what's the chapter and verse for your claim?

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-10 14:20         ` sizeof Russell Shaw
  2022-05-10 14:48           ` sizeof Andreas Schwab
@ 2022-05-10 15:43           ` Russell Shaw
  2022-05-10 15:48           ` sizeof Russell Shaw
  2 siblings, 0 replies; 15+ messages in thread
From: Russell Shaw @ 2022-05-10 15:43 UTC (permalink / raw)
  To: gdb, Andreas Schwab

Andreas Schwab wrote:
 >On Mai 11 2022, Russell Shaw wrote:
 >
 >> On 10/5/22 5:22 pm, Andreas Schwab wrote:
 >>> On Mai 10 2022, Russell Shaw wrote:
 >>>
 >>>>> Andreas Schwab wrote:
 >>>>> On Mai 09 2022, Russell Shaw wrote:
 >>>>>
 >>>>>> sizeof(int()) should transform to sizeof(int (*)()) according to the 
C++20 standard.
 >>>>>
 >>>>> No, see [expr.sizeof] #3.
 >>>>
 >>>> What does "[expr.sizeof] #3" refer to ?
 >>> Paragrph #3 in section [expr.sizeof].
 >>>      "The lvalue-to-rvalue (7.3.1), array-to-pointer (7.3.2), and
 >>>      function-to-pointer (7.3.3) standard conversions are not applied to
 >>>      the operand of sizeof."
 >>
 >> "Standard conversions" are only applied to expressions, not type-id (which
 >> is an abstract declarator syntax).
 >
 >So what's the chapter and verse for your claim?

It is not spelt out, but implied from the context.

Clearly, the standard conversions are only a meaningful concept for expressions.

"int()" is not an expression, but a type-id

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-10 14:20         ` sizeof Russell Shaw
  2022-05-10 14:48           ` sizeof Andreas Schwab
  2022-05-10 15:43           ` sizeof Russell Shaw
@ 2022-05-10 15:48           ` Russell Shaw
  2022-05-10 16:06             ` sizeof Andreas Schwab
  2022-05-11  5:38             ` sizeof Russell Shaw
  2 siblings, 2 replies; 15+ messages in thread
From: Russell Shaw @ 2022-05-10 15:48 UTC (permalink / raw)
  To: gdb, Andreas Schwab

Andreas Schwab wrote:
>On Mai 11 2022, Russell Shaw wrote:
>
>> On 10/5/22 5:22 pm, Andreas Schwab wrote:
>>> On Mai 10 2022, Russell Shaw wrote:
>>>
>>>>> Andreas Schwab wrote:
>>>>> On Mai 09 2022, Russell Shaw wrote:
>>>>>
>>>>>> sizeof(int()) should transform to sizeof(int (*)()) according to the 
C++20 standard.
>>>>>
>>>>> No, see [expr.sizeof] #3.
>>>>
>>>> What does "[expr.sizeof] #3" refer to ?
>>> Paragrph #3 in section [expr.sizeof].
>>>      "The lvalue-to-rvalue (7.3.1), array-to-pointer (7.3.2), and
>>>      function-to-pointer (7.3.3) standard conversions are not applied to
>>>      the operand of sizeof."
>>
>> "Standard conversions" are only applied to expressions, not type-id (which
>> is an abstract declarator syntax).
>
>So what's the chapter and verse for your claim?

It is not spelt out, but implied from the context.

Clearly, the standard conversions are only a meaningful concept for expressions.

"int()" is not an expression, but a type-id for a function pointer "int(*)()"

so "sizeof(int())" == "size of ptr to function"

unary-expression :
   ...
   sizeof unary-expression
   sizeof ( type-id )

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-10 15:48           ` sizeof Russell Shaw
@ 2022-05-10 16:06             ` Andreas Schwab
  2022-05-11  5:38             ` sizeof Russell Shaw
  1 sibling, 0 replies; 15+ messages in thread
From: Andreas Schwab @ 2022-05-10 16:06 UTC (permalink / raw)
  To: Russell Shaw; +Cc: gdb

On Mai 11 2022, Russell Shaw wrote:

> "int()" is not an expression, but a type-id for a function pointer "int(*)()"

Where does it say that?

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-10 15:48           ` sizeof Russell Shaw
  2022-05-10 16:06             ` sizeof Andreas Schwab
@ 2022-05-11  5:38             ` Russell Shaw
  2022-05-11  8:16               ` sizeof Russell Shaw
  1 sibling, 1 reply; 15+ messages in thread
From: Russell Shaw @ 2022-05-11  5:38 UTC (permalink / raw)
  To: gdb, Andreas Schwab

Andreas Schwab wrote:
 >On Mai 11 2022, Russell Shaw wrote:
 >
 >> "int()" is not an expression, but a type-id for a function pointer "int(*)()"
 >
 >Where does it say that?

*****************
7.2 Properties of expressions
7.2.1 Value category
2) Every expression belongs to exactly one of the fundamental classifications in 
this taxonomy: lvalue, xvalue, or prvalue. This property of an expression is 
called its value category.
*****************

Does "int()" look like a lvalue, xvalue, or prvalue ?


*****************
9.3.3 Ambiguity resolution [dcl.ambig.res]
1)
...
Just as for the ambiguities mentioned in 8.9, the resolution is to consider any 
construct that matches the syntax of a declaration to be a declaration.
*****************

Note that a type-id is a declaration syntax.


The parameter "int()" of "void f(int())" is a type-id because:

template-parameter :
   type-parameter

type-parameter :
   type-parameter-key identifier(opt) = type-id


Therefore, decaying "int()" to "int(*)()" is not performed on an expression, so 
none of the prohibitions about standard conversions (an expression concept) applies.



*****************
9.3.3 Ambiguity resolution [dcl.ambig.res]
2)
...
void foo(signed char a)
{
   sizeof(int());                // type-id (ill-formed)
   ...
}
*****************

The standard has an error because "int()" is a valid type-id

void f(int());

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-11  5:38             ` sizeof Russell Shaw
@ 2022-05-11  8:16               ` Russell Shaw
  2022-05-11 17:44                 ` sizeof Russell Shaw
  0 siblings, 1 reply; 15+ messages in thread
From: Russell Shaw @ 2022-05-11  8:16 UTC (permalink / raw)
  To: gdb, Andreas Schwab

int bar() {
     return 0;
}

template<class T = int()> void foo()
{
     T p = bar;
}

int
main(int argc, char *argv[])
{
     foo();
}

g++ ...
main.cpp: In instantiation of ‘void foo() [with T = int()]’:
main.cpp:13:9:   required from here
main.cpp:7:7: error: variable ‘p’ has function type
     7 |     T p = bar;
       |       ^
***************************************************

If the "int()" type-id decayed to "int(*)()", this would work.

"sizeof(int()) => 1" just seems wrong, but maybe it's a matter of taste that has 
been explicitly decided.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: sizeof
  2022-05-11  8:16               ` sizeof Russell Shaw
@ 2022-05-11 17:44                 ` Russell Shaw
  0 siblings, 0 replies; 15+ messages in thread
From: Russell Shaw @ 2022-05-11 17:44 UTC (permalink / raw)
  To: gdb, Andreas Schwab

On 11/5/22 18:16, Russell Shaw wrote:
...
> "sizeof(int()) => 1" just seems wrong, but maybe it's a matter of taste that has 
> been explicitly decided.

Despite all the proceeding, i don't think "sizeof(int())" should be sizeof ptr 
to function now. The objective of sizeof is for finding the size an item will be 
in an array of the items.

sizeof(int[3]);  => 12
sizeof(int());   => illegal

"int()" is a legal type-id for a function, but you can't have an array of 
functions. "int()" could be decayed to a ptr "int(*)()", but that would be 
inconsistant with "int[3]" that you wouldn't want to decay.

So i conclude that g++ is right by saying "warning: invalid application of 
‘sizeof’ to a function type", and gdb should not say "sizeof(int())" => 1

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-05-11 17:44 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-08 15:22 sizeof Russell Shaw
2022-05-08 17:29 ` sizeof Simon Marchi
2022-05-08 17:48   ` sizeof Andreas Schwab
2022-05-09  7:21   ` sizeof Russell Shaw
2022-05-09  7:35     ` sizeof Andreas Schwab
2022-05-10  3:46     ` sizeof Russell Shaw
2022-05-10  7:22       ` sizeof Andreas Schwab
2022-05-10 14:20         ` sizeof Russell Shaw
2022-05-10 14:48           ` sizeof Andreas Schwab
2022-05-10 15:43           ` sizeof Russell Shaw
2022-05-10 15:48           ` sizeof Russell Shaw
2022-05-10 16:06             ` sizeof Andreas Schwab
2022-05-11  5:38             ` sizeof Russell Shaw
2022-05-11  8:16               ` sizeof Russell Shaw
2022-05-11 17:44                 ` sizeof Russell Shaw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).