static_cast<float>(a) versus float(a)

Discussion in 'C++' started by Jim West, Jan 15, 2004.

  1. Jim West

    Jim West Guest

    Both

    int a;
    std::complex<float> b;
    std::complex<float> c = static_cast<float>(a)*b;

    and

    int a;
    std::complex<float> b;
    std::complex<float> c = float(a)*b;

    compile to exactly the same assembler on two different compilers.
    However, I always seem to miss the subtleties of the language. Is
    one version to be preferred over the other? I tend to use the latter
    since it is more compact, which based on my track record is probably
    the wrong way to go. :)

    Stylistic suggestions greatly appreciated.
    Jim West, Jan 15, 2004
    #1
    1. Advertising

  2. Jim West

    Ron Natalie Guest

    "Jim West" <> wrote in message news:...

    > compile to exactly the same assembler on two different compilers.
    > However, I always seem to miss the subtleties of the language. Is
    > one version to be preferred over the other? I tend to use the latter
    > since it is more compact, which based on my track record is probably
    > the wrong way to go. :)
    >

    They both are required to do the same thing. The C cast prefers the
    static_cast behavior over reinterpret_cast. Of course unless you're
    dealing with pointers or references, the reinterpret_cast won't ever fire
    so it's pretty safe.
    Ron Natalie, Jan 15, 2004
    #2
    1. Advertising

  3. Jim West wrote:
    > Both
    >
    > int a;
    > std::complex<float> b;
    > std::complex<float> c = static_cast<float>(a)*b;
    >
    > and
    >
    > int a;
    > std::complex<float> b;
    > std::complex<float> c = float(a)*b;
    >
    > compile to exactly the same assembler on two different compilers.
    > However, I always seem to miss the subtleties of the language. Is
    > one version to be preferred over the other?


    Both versions do the same thing. One should be preffered just for the
    sake of consistency, but which one should that be is purely a matter of
    personal choice.

    > I tend to use the latter
    > since it is more compact, which based on my track record is probably
    > the wrong way to go. :)


    No, in this context there's nothing wrong with it. I, for example,
    prefer using "old" C-style casts for arithmetical conversions.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 15, 2004
    #3
  4. Jim West

    PETER_ Guest

    Jim West <> wrote in message news:<>...
    > Both
    >
    > int a;
    > std::complex<float> b;
    > std::complex<float> c = static_cast<float>(a)*b;
    >
    > and
    >
    > int a;
    > std::complex<float> b;
    > std::complex<float> c = float(a)*b;
    >
    > compile to exactly the same assembler on two different compilers.
    > However, I always seem to miss the subtleties of the language. Is
    > one version to be preferred over the other? I tend to use the latter
    > since it is more compact, which based on my track record is probably
    > the wrong way to go. :)
    >
    > Stylistic suggestions greatly appreciated.


    If I recall correctly using the c-style cast instead of the c++ style
    cast can
    allow for a reinterpret_cast<TYPETARGET>(TYPESOURCE) to occur when the
    correct
    conversion operators or constructors are unavailable. For built-in
    types this may not be true.

    However, your code snippet seems to indicate using C++ conversion
    features and therefore your codes intent might be to require explicit
    conversion rather than compiler defaulted conversion. If instead of
    "int a" it was "MyClass a" the static_cast would be more specific
    that you were expecting a conversion operation and not a bitwise
    interpretation of the "a" bits.
    PETER_, Jan 16, 2004
    #4
  5. Jim West

    Jeff Schwab Guest

    Jim West wrote:
    > Both
    >
    > int a;
    > std::complex<float> b;
    > std::complex<float> c = static_cast<float>(a)*b;
    >
    > and
    >
    > int a;
    > std::complex<float> b;
    > std::complex<float> c = float(a)*b;
    >
    > compile to exactly the same assembler on two different compilers.
    > However, I always seem to miss the subtleties of the language. Is
    > one version to be preferred over the other? I tend to use the latter
    > since it is more compact, which based on my track record is probably
    > the wrong way to go. :)
    >
    > Stylistic suggestions greatly appreciated.


    I tend to prefer the first, so I can grep for casts.

    "(float)a" of course remains hideous. :)
    Jeff Schwab, Jan 16, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    624
  2. Bo Peng
    Replies:
    11
    Views:
    1,085
    Victor Bazarov
    Oct 20, 2006
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,540
    James Kanze
    Oct 8, 2009
  4. Paul Butcher
    Replies:
    12
    Views:
    710
    Gary Wright
    Nov 28, 2007
  5. junyangzou
    Replies:
    13
    Views:
    257
Loading...

Share This Page