Home > Software design >  Where does C 20 prohibit sizeof...T (without parentheses)
Where does C 20 prohibit sizeof...T (without parentheses)

Time:06-02

Both g and clang reject the use of sizeof... when the argument is not parenthesized. For example, the following code is rejected:

template<typename... T> auto packsize = sizeof...T;
$ g    -std=c  20 -ggdb -O -Wall -Werror   -c -o z.o z.cc
z.cc:1:50: error: 'sizeof...' argument must be surrounded by parentheses [-fpermissive]
    1 | template<typename... T> auto packsize = sizeof...T;
      |                                                  ^
$ clang    -std=c  20 -ggdb -O -Wall -Werror   -c -o z.o z.cc
z.cc:1:50: error: missing parentheses around the size of parameter pack 'T'
template<typename... T> auto packsize = sizeof...T;
                                                 ^
                                                 ()
1 error generated.

CPPreference.org also seems to require parentheses around T. However, such a restriction does not appear in any of the obvious places in the C 20 standard, e.g., the discussion of sizeof... or the discussion of pack expansions. However, all non-normative examples in the standard do include parentheses.

My question: what normative language prohibits the use of sizeof... with an unparenthesized identifier in C 20?

CodePudding user response:

It's required by the grammar:

[expr.unary.general]/1

unary-expression:
      ...
      sizeof ... ( identifier )
      ...

  • Related