old-style simple cast precedence in c++

Viewed 1319

I have some old-style casting in some c++ code which I would like to convert to new-style. I had a look to precedence and associativity operators documentation, but I failed to understand it.

( double ) myValueA() / myValueB()

is equivalent to

static_cast<double>( myValueA() ) / myValueB()

or to

static_cast<double>( myValueA() / myValueB() )

I suppose the answer will the same for other numerical operators (*/+-)

2 Answers

In

( double ) myValueA() / myValueB()

( double ) is a c-style cast. If we look at the operator precedence table we will see that it has a higher precedence than the arithmetic operators so

( double ) myValueA() / myValueB()

is the same as

static_cast<double>(myValueA()) / myValueB()

The cast has higher precendence, so it is equivalent to

static_cast<double>(myValueA()) / myValueB()
Related