Search code examples
c++castingoperator-precedencestatic-cast

old-style simple cast precedence in c++


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 (*/+-)


Solution

  • 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()