c++ - Are there differences between explicit type conversion "operators" in case of basic types? -
simple thing: "convert" e.g. float
double
. there 3 ways known me:
float v = 4.2f;
double u = (double)v;
double u = double(v);
double u = static_cast<double>(v);
double u(v);
edit: thought fourth option!
are these identical or there subtle differences? suggest use?
note question related basic types int, char, float, ... not pointers, pods or classes.
double u = (double)v;
, double u = static_cast<double>(v);
equivalent because both cases standard conversion used. double u = double(v);
creates temporary double object (which can optimized away anyway) used initialize u
. since temporaty created anyway, using 3 kinds of casts, yeah, it's same.
from 3 static_cast
should preferred. it's couple of characters more typing, in long run it's better because first of explicitly specify cast type , since casting suspicious, in vivid manner
Comments
Post a Comment