Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 590
Denis Rouzaud Avatar asked Sep 19 '18 16:09

Denis Rouzaud


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()
like image 79
NathanOliver Avatar answered Oct 05 '22 23:10

NathanOliver


The cast has higher precendence, so it is equivalent to

static_cast<double>(myValueA()) / myValueB()
like image 39
Parker Coates Avatar answered Oct 06 '22 01:10

Parker Coates