First, sizeof is not a function but an operator
sizeof(0)["abcdefghij"]
can be parsed as either
sizeof( (0)["abcdefghij"] )
, or
( sizeof(0) )["abcdefghij"]
Since sizeof
has lower precedence than []
, the former will take place
(0)["abcdefghij"]
is equivalent to "abcdefghij"[0]
which is just 'a'
, so the whole thing is the same as sizeof('a')
which is 1 in C++
Demo on GodBolt, ideone
If you replace sizeof(0)
with sizeof(int)
then the same thing happens, but now (int)["abcdefghij"]
is invalid so it should result in a compilation fail. Most compilers report an error as expected that except ICC so it looks like that's an ICC bug which chooses (sizeof(int))["abcdefghij"]
over sizeof((int)["abcdefghij"])
just because the latter is invalid
Related: Why does sizeof(my_arr)[0] compile and equal sizeof(my_arr[0])?
"abcdefghij"
is justconst char [N]
, i.e. an array, so it's equivalent tosizeof("abcdefghij"[0])
becausesizeof
has higher precedence than[]
sizeof("..."[0])
.