Search code examples
c++language-lawyervariadic-templatesc++20parameter-pack

Where does C++20 prohibit sizeof...T (without parentheses)


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?


Solution

  • It's required by the grammar:

    [expr.unary.general]/1

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