Sunday, 15 June 2014

c++ - How is this boolean expression evaluated? -



c++ - How is this boolean expression evaluated? -

in c++, allow next build :

template<typename valuetype> valuetype * func(foo * foo) { bar bar; homecoming foo && typeid(foo) == typeid(valuetype) ? &static_cast<valuetype*>bar : 0; }

how homecoming statement evaluated? so?

if ((bar && typeid(bar)) == typeid(valuetype)) homecoming &static_cast<valuetype*>bar homecoming false;

foo && typeid(foo) == typeid(valuetype) ? &static_cast<valuetype*>bar : 0;

...corrected parenthesis after static_cast<>, evaluated as...

(foo && (typeid(foo) == typeid(valuetype))) ? (&(static_cast<valuetype*>(bar))) : 0;

the precedence rules listed here. notice ?: ternary operator @ precedence level 15 in list - lower other operators you've used, defines outer construction of evaluation. && @ 13 - way below == @ 9. (i don't think these numbers used anywhere in standard, they're convenient references pointing out things in cppreference table).

c++

No comments:

Post a Comment