Quantcast
Viewing latest article 11
Browse Latest Browse All 65

Answer by CPlus for Types and variables in a different namespace

Function call or type cast?

One example of an ambiguous statement in a C-style language that supports type names and object names overlapping is if there is a function or function pointer called x and a type called x.

The expression (x)(10) would be ambiguous because there is no way of knowing just by looking if I am calling the x object with 10 as an argument, or if I am casting 10 to type x.

One way to disambiguate is to use an extra set of parenthesis around the x like ((x))(10). This would unambiguously refer to the function call because as there is no expression after (x) in this case so there is no cast.

Types cannot be 'parenthesized' by themselves by putting parenthesis around an ambiguous name so we are forcing the compiler to treat the name as an expression.

As such, in ambiguous cases, the type name can be the 'default.' And to opt into the expression version, we can place parenthesis around the type.


Viewing latest article 11
Browse Latest Browse All 65

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>