Is static_cast really as fast as C/C++ style casts?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • esuvs81@googlemail.com

    Is static_cast really as fast as C/C++ style casts?

    Hi all. In short, is there any performance difference between:

    float f = 10.0f;
    int i = static_cast<int >(f);

    and

    float f = 10.0f;
    int i = int(f);

    I've been meaning to ask this for a while but just how fast is
    static_cast? I had always assumed (without proof) that static_cast is
    implemented as a template which just wraps a C/C++ style cast.But then
    it the Josuttis book it says:

    "The conversion is allowed only if a type conversion is defined"

    Presumably then this test is done at compile time?

    Also, if static_cast is just a wrapper around C/C++ casts then how does
    it differ from reinterpret_cas t? Maybe these things are compiler
    dependant but any info is useful.

    Thanks,

    David

  • Frederick Gotham

    #2
    Re: Is static_cast really as fast as C/C++ style casts?

    Hi all. In short, is there any performance difference between:
    >
    float f = 10.0f;
    int i = static_cast<int >(f);
    >
    and
    >
    float f = 10.0f;
    int i = int(f);

    No, because a "cast" is a compile-time concept -- not a runtime concept.
    Both should produce identical machine code.

    I've been meaning to ask this for a while but just how fast is
    static_cast?

    It doesn't have a speed.

    I had always assumed (without proof) that static_cast is implemented as
    a template which just wraps a C/C++ style cast.But then it the Josuttis
    book it says:
    >
    "The conversion is allowed only if a type conversion is defined"
    >
    Presumably then this test is done at compile time?

    Yes it is. Test it with a conforming compiler:

    char *p = 0;

    long i = static_cast<lon g>(p);

    You should get a compiler error.
    Also, if static_cast is just a wrapper around C/C++ casts then how does
    it differ from reinterpret_cas t? Maybe these things are compiler
    dependant but any info is useful.

    In the way I showed you above. There's a list of things which static_cast
    WILL let you do, and others which it WON'T let you do. For the things it
    won't let you do, you must use reinterpret_cas t.

    --

    Frederick Gotham

    Comment

    • loufoque

      #3
      Re: Is static_cast really as fast as C/C++ style casts?

      esuvs81@googlem ail.com wrote :
      Presumably then this test is done at compile time?
      Yes.
      >

      Comment

      • esuvs81@googlemail.com

        #4
        Re: Is static_cast really as fast as C/C++ style casts?

        Ok, thanks for the input. I'm keen to write correct code but don't want
        to sacrifice speed if possible. Seems like I'm ok here :-D

        esuvs81@googlem ail.com wrote:
        Hi all. In short, is there any performance difference between:
        >
        float f = 10.0f;
        int i = static_cast<int >(f);
        >
        and
        >
        float f = 10.0f;
        int i = int(f);
        >
        I've been meaning to ask this for a while but just how fast is
        static_cast? I had always assumed (without proof) that static_cast is
        implemented as a template which just wraps a C/C++ style cast.But then
        it the Josuttis book it says:
        >
        "The conversion is allowed only if a type conversion is defined"
        >
        Presumably then this test is done at compile time?
        >
        Also, if static_cast is just a wrapper around C/C++ casts then how does
        it differ from reinterpret_cas t? Maybe these things are compiler
        dependant but any info is useful.
        >
        Thanks,
        >
        David

        Comment

        • Thomas J. Gritzan

          #5
          Re: Is static_cast really as fast as C/C++ style casts?

          esuvs81@googlem ail.com schrieb:
          Ok, thanks for the input. I'm keen to write correct code but don't want
          to sacrifice speed if possible. Seems like I'm ok here :-D
          Read this interesting article:


          --
          Thomas

          Comment

          • Julián Albo

            #6
            Re: Is static_cast really as fast as C/C++ style casts?

            Frederick Gotham wrote:
            No, because a "cast" is a compile-time concept -- not a runtime concept.
            Not always, think about dynamic_cast. Someone can debate if is the same
            concept of casting that the others cast, but his name in C++ is clear.

            --
            Salu2

            Comment

            • Pete Becker

              #7
              Re: Is static_cast really as fast as C/C++ style casts?

              Julián Albo wrote:
              Frederick Gotham wrote:
              >
              >No, because a "cast" is a compile-time concept -- not a runtime concept.
              >
              Not always, think about dynamic_cast. Someone can debate if is the same
              concept of casting that the others cast, but his name in C++ is clear.
              >
              A cast is something that exists in source code to tell the compiler to
              do a conversion. Some conversions can be done implicitly, and others
              will only be done if you tell the compiler with a cast. Some conversions
              require runtime code, but that's independent of whether the conversion
              requires a cast. In some circumstances, dynamic_cast does not require
              modifying the pointer value. In some circumstances, static_cast does. In
              some circumstances, initializing a pointer to base from a pointer to
              derived (without a cast) modifies the pointer value; in some it does not.

              --

              -- Pete

              Author of "The Standard C++ Library Extensions: a Tutorial and
              Reference." For more information about this book, see
              www.petebecker.com/tr1book.

              Comment

              • Pete Becker

                #8
                Re: Is static_cast really as fast as C/C++ style casts?

                esuvs81@googlem ail.com wrote:
                >
                I've been meaning to ask this for a while but just how fast is
                static_cast? I had always assumed (without proof) that static_cast is
                implemented as a template which just wraps a C/C++ style cast.
                >
                static_cast is not a template. It's a keyword, and the compiler
                generates whatever code is appropriate. The difference between
                static_cast and a C-style cast is that there are some conversions that
                you can do with a C-style cast that you can't do with a static_cast.

                --

                -- Pete

                Author of "The Standard C++ Library Extensions: a Tutorial and
                Reference." For more information about this book, see
                www.petebecker.com/tr1book.

                Comment

                • Julián Albo

                  #9
                  Re: Is static_cast really as fast as C/C++ style casts?

                  Pete Becker wrote:
                  >>No, because a "cast" is a compile-time concept -- not a runtime concept.
                  >Not always, think about dynamic_cast. Someone can debate if is the same
                  >concept of casting that the others cast, but his name in C++ is clear.
                  >
                  A cast is something that exists in source code to tell the compiler to
                  do a conversion. Some conversions can be done implicitly, and others
                  will only be done if you tell the compiler with a cast. Some conversions
                  require runtime code, but that's independent of whether the conversion
                  requires a cast. In some circumstances, dynamic_cast does not require
                  modifying the pointer value. In some circumstances, static_cast does. In
                  But the runtime nature of dynamic_cast is more relevant. With other casts
                  you essentially say "I know the conversion is valid". With dynamic_cast you
                  say "check at runtime if the conversion is possible".

                  --
                  Salu2

                  Comment

                  • Pete Becker

                    #10
                    Re: Is static_cast really as fast as C/C++ style casts?

                    Julián Albo wrote:
                    Pete Becker wrote:
                    >
                    >>>No, because a "cast" is a compile-time concept -- not a runtime concept.
                    >>Not always, think about dynamic_cast. Someone can debate if is the same
                    >>concept of casting that the others cast, but his name in C++ is clear.
                    >A cast is something that exists in source code to tell the compiler to
                    >do a conversion. Some conversions can be done implicitly, and others
                    >will only be done if you tell the compiler with a cast. Some conversions
                    >require runtime code, but that's independent of whether the conversion
                    >requires a cast. In some circumstances, dynamic_cast does not require
                    >modifying the pointer value. In some circumstances, static_cast does. In
                    >
                    But the runtime nature of dynamic_cast is more relevant. With other casts
                    you essentially say "I know the conversion is valid". With dynamic_cast you
                    say "check at runtime if the conversion is possible".
                    I just re-read what I wrote, and, unfortunately, left out something
                    important: sometimes a dynamic_cast does not require any runtime check.
                    Converting a pointer to T into a pointer to T, for a silly example. But
                    you can also use a dynamic_cast to convert a pointer to derived into a
                    pointer to base, and that needs only a compile-time check.

                    --

                    -- Pete

                    Author of "The Standard C++ Library Extensions: a Tutorial and
                    Reference." For more information about this book, see
                    www.petebecker.com/tr1book.

                    Comment

                    • Julián Albo

                      #11
                      Re: Is static_cast really as fast as C/C++ style casts?

                      Pete Becker wrote:
                      >>>>No, because a "cast" is a compile-time concept -- not a runtime
                      >>>>concept.
                      >>>Not always, think about dynamic_cast. Someone can debate if is the same
                      (...)
                      important: sometimes a dynamic_cast does not require any runtime check.
                      Converting a pointer to T into a pointer to T, for a silly example. But
                      you can also use a dynamic_cast to convert a pointer to derived into a
                      pointer to base, and that needs only a compile-time check.
                      Yes, but the point in discussion was that a cast in C++ is not always a
                      compile time concept, and the facts you mention does not contradict it.
                      Maybe I loose some topic change?

                      --
                      Salu2

                      Comment

                      • Pete Becker

                        #12
                        Re: Is static_cast really as fast as C/C++ style casts?

                        Julián Albo wrote:
                        Pete Becker wrote:
                        >
                        >>>>>No, because a "cast" is a compile-time concept -- not a runtime
                        >>>>>concept.
                        >>>>Not always, think about dynamic_cast. Someone can debate if is the same
                        (...)
                        >important: sometimes a dynamic_cast does not require any runtime check.
                        >Converting a pointer to T into a pointer to T, for a silly example. But
                        >you can also use a dynamic_cast to convert a pointer to derived into a
                        >pointer to base, and that needs only a compile-time check.
                        >
                        Yes, but the point in discussion was that a cast in C++ is not always a
                        compile time concept, and the facts you mention does not contradict it.
                        Maybe I loose some topic change?
                        >
                        A cast is something you write in your source code. A conversion is what
                        the compiler does in response, as well as in many situations where a
                        cast is not required. Conversions sometimes have runtime consequences,
                        regardless of whether they result from casts. Conflating "cast" with
                        "conversion " muddles things.

                        --

                        -- Pete

                        Author of "The Standard C++ Library Extensions: a Tutorial and
                        Reference." For more information about this book, see
                        www.petebecker.com/tr1book.

                        Comment

                        Working...