Immanuel said:
hall said:
Out of curiosity, is this behaviour guarantied by the standard? Is
getline forced (by the standard) to read a string that is never used
or is it possible for the compiler to remove this as an optimization?
It is not allowed to remove the read by optimization, because it changes
the whole effect that your program has (i.e. no keyboard input read->
different system state).
If so, you could never read a dummy line that contains no valuable
information for your program.
Does the same answer hold for other ways of reading a string (or other
data) from a stream object (eg [int i; std::cin >>i] , [string s; cin
Of course. You optimizer will not decide whether your code makes sense
and erase the code if not. That's what the programmers are for.
unless you are using Fortran
I guess your optimizer then was broken. I do not think it is legal to
remove program output even in Fortran. Its like you write a program
calculating pi to the 4050493493...4993..32nd digit and then your
optimizer doesn't make the output. See?
Actually, no. The compiler was not broken. Optimizing can be(*) a rather
violent process in Fortran and may remove functionality in the code,
resulting in missing outputs, ignored function calls etc. And this is of
course why I wanted to make sure that C++ did not support anything like
this in its standard.
However, code such as
for (unsigned long int i=0; i<255^4; i++){
float f=... // silly calculations affecting variables
// only within the for-loop scope
}
should be removed by any decent optimization algorithm in C++ (and
programmer to, for that matter), right?
/hall
(*)perhaps I should add that how far the optimization is allowed to go
in Fortran can be controlled by setting the options for it. It doesn't
go wild on your code everytime ;-)