473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

static_cast usage

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
--
Gary

Jul 19 '05 #1
7 6335
In article <F5********************@comcast.com>, gl*******@comcast.net
says...

[ ... ]
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;

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #2
> 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;

--
Phlip
Jul 19 '05 #3
Phlip wrote in news:N2***************@newssvr17.news.prodigy.com:
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;


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

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

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.204...
Phlip wrote in news:N2***************@newssvr17.news.prodigy.com:
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;


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!
--
Gary
Jul 19 '05 #5
Gary Labowitz wrote:
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

Jul 19 '05 #6
"Christian Brechbühler" <c_************@yhaoo.com> wrote in message
news:7XJgb.707441$uu5.116817@sccrnsc04...
Gary Labowitz wrote:
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.


Oh, oh. I hate to admit it on this one, but I'm the teacher!
I ask them to use static cast.
--
Gary
Jul 19 '05 #7
> > > cout << static_cast<double> (x)/y<<endl;
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.)

--
Phlip
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: alg | last post by:
I read somewhere that static_cast<> is not safe, sometimes fatal. Could someone tell me why it is still around and when it should be used in general? When will it be unsafe to use it? Thanks...
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
1
by: Alex Vinokur | last post by:
Here is some program in which static_cast is (wrongly) used instead of dynamic_cast. Is output of the program undefined or predicted? =========== C++ code : foo.cpp : BEGIN ===========...
13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
2
by: Amit | last post by:
Greetings. I am having some problem while using a cast operation(static_cast and/or dynamic_cast) between base and derived objects when passing to functions. what I have is something like this.. ...
1
by: PengYu.UT | last post by:
Hi, An example usage of static_cast is at http://publib.boulder.ibm.com/infocenter/macxhelp/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc05keyword_static_cast.htm But I don't...
24
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.