5

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?

user3188445
  • 4,062
  • 16
  • 26
  • 2
    This is consistent with the non-... version, where `sizeof X` is valid when `X` is an object, but not when it's a type; with a type the parentheses are required, so `sizeof(X)`. – Pete Becker Jun 01 '22 at 21:45
  • @PeteBecker Not really, you could have a non-type pack and would still need parentheses. – HolyBlackCat Jun 02 '22 at 17:17

1 Answers1

12

It's required by the grammar:

[expr.unary.general]/1

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

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207