static_cast usage

J

Jerry Coffin

[ ... ]
cout << static_cast<double> x/y<<endl;

You have to put the expression being cast into parentheses:

std::cout << static_cast<double>(x)/y << std::endl;
 
G

Gary Labowitz

Am I doing this correctly? It is a sample program for my class.

#include <iostream>

using namespace std;

int main( )
{
int x=3, y=4;

cout << "using cast ";
cout << static_cast<double> x/y<<endl;

return 0;
}

Compiled with Dev-C++ using minGW compiler I get

parse error before '/' token

on line 10 indicating the static_cast. Changing to (double) runs okay. Is
this a problem with minGW, g++, Dev-C++, me? I have checked archives of all
mailing lists and see nothing there. Compile line is
g++.exe "D:\DPR226\My Files\Untitled2.cpp" -o "D:\DPR226\My
Files\Untitled2.exe" -l
"D:\Dev-Cpp\include\c++" -l"D:\Dev-Cpp\include\c++\mingw32" -l"D:\Dev-Cpp\in
clude" -l"D:\Dev-Cpp\include\c++\backward" -L"D:\Dev-Cpp\lib"

I don't see it. TIA
 
P

Phlip

cout << static_cast<double> x/y<<endl;

Try:

cout << static_cast<double> (x)/y<<endl;

However, as a style thing, in this case the static_cast-ed numerator
overwhelms the denominator, making it feel small.

Just use a constructor cast:

cout << double(x) / y <<endl;

I would also not trust the precedence of << over /. Try this:

double xy (double(x) / y);
cout << xy << endl;
 
R

Rob Williscroft

Phlip wrote in
Try:

cout << static_cast<double> (x)/y<<endl;

However, as a style thing, in this case the static_cast-ed numerator
overwhelms the denominator, making it feel small.

Just use a constructor cast:

cout << double(x) / y <<endl;

Assuming you don't have a broken optimizer you can also go "cast free".

cout << (x * 1.0 / y) << endl;

Rob.
 
G

Gary Labowitz

Rob Williscroft said:
Phlip wrote in

Assuming you don't have a broken optimizer you can also go "cast free".

cout << (x * 1.0 / y) << endl;

Egad, I'm an idiot. So static_cast requires the expression to be in
parentheses, eh?
Thanks for all the answers, they are very creative. Of the lot, I think I
prefer static_cast<double>(x) best since it seems more in line with C++
using static_cast.
I did use (double)x and the 1.0 multiplier as well, but they avoid the
issue.

This raises another issue: do you really prefer double(x) to (double)x?
[Phlip] It does follow the structure of the static_cast form.

No wonder the archives search didn't find anything!
 
?

=?ISO-8859-1?Q?Christian_Brechb=FChler?=

Gary said:
This raises another issue: do you really prefer double(x) to (double)x?

Absolutely. The latter is a C-style cast and I avoid it. In my
opinion, double(x) expresses the conversion most clearly -- unless your
assignment explicitly asked you to use a static cast.

Christian
 
G

Gary Labowitz

Christian Brechbühler said:
Absolutely. The latter is a C-style cast and I avoid it. In my
opinion, double(x) expresses the conversion most clearly -- unless your
assignment explicitly asked you to use a static cast.

Oh, oh. I hate to admit it on this one, but I'm the teacher!
I ask them to use static cast.
 
P

Phlip

cout said:
No wonder the archives search didn't find anything!

That right there is a canonical reason to use elaborate_cast. You can
search for it in your code.

(Another reason is they generally constrain their argument types such
that attempts to change the cast into what a () C style cast would
recognize as a different kind of elaborate_cast cause an error.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top