7

In [dcl.ambig.res]/2 we find the following:

void foo(signed char a) {
    sizeof(int(a)); // expression
    sizeof(int(unsigned(a))); // type-id (ill-formed)
}

Why is int(a) an expression and int(unsigned(a)) a type-id?

At first sight I would say that both are expressions.

Boann
  • 48,794
  • 16
  • 117
  • 146
Alexander
  • 2,581
  • 11
  • 17
  • Possible duplicate of [A confusing detail about the Most Vexing Parse](https://stackoverflow.com/questions/7007817/a-confusing-detail-about-the-most-vexing-parse) – Arne Vogel Mar 29 '18 at 09:42

1 Answers1

8

int(unsigned(a)) is parsed the same as int(unsigned a), which is a function type

Justin
  • 24,288
  • 12
  • 92
  • 142
  • 3
    See now _this_ is the most vexing parse. – Lightness Races in Orbit Mar 28 '18 at 22:09
  • 1
    And see the comments to https://stackoverflow.com/a/6342009/179715 for *why* it's parsed that way. – jamesdlin Mar 28 '18 at 22:11
  • @LightnessRacesinOrbit Even more vexing: use `decltype` instead of `sizeof` and [it's treated as an expression of type `int`](https://godbolt.org/g/dfNJ4E). I'm not certain that I'm actually correct here, though [the warning gcc gives](https://godbolt.org/g/3gjgkv) seems to imply that my reasoning here was correct – Justin Mar 28 '18 at 22:13
  • 1
    Lol. You may be, or may not be. I dunno. I gave up on this kind of thing a few years ago. Life's too short :) (In 2014 I might have said "that kinda looks like a compiler bug to me") – Lightness Races in Orbit Mar 28 '18 at 22:15
  • 1
    I agree it is particularly vexing that a type-id is allowed to contain function parameter names... – Brian Bi Mar 28 '18 at 22:24
  • @Brian Redundant function parameter names have been allowed since ancient C as well, e.g. `typedef int (*FPTR)(int value);`, and they can be very useful for readability in some cases, e.g. `using log_func = double(double base, double value)`. It's also consistent with the fact that parameter names are sort of redundant anyway in a (non-defining) function/method declaration. It does compound to the MVP though. – Arne Vogel Mar 29 '18 at 09:52