static_cast usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Labowitz

    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<dou ble> 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



  • Jerry Coffin

    #2
    Re: static_cast usage

    In article <F5edncoCwNFE Vh-iU-KYgw@comcast.co m>, glabowitz@comca st.net
    says...

    [ ... ]
    [color=blue]
    > cout << static_cast<dou ble> x/y<<endl;[/color]

    You have to put the expression being cast into parentheses:

    std::cout << static_cast<dou ble>(x)/y << std::endl;

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.

    Comment

    • Phlip

      #3
      Re: static_cast usage

      > cout << static_cast<dou ble> x/y<<endl;

      Try:

      cout << static_cast<dou ble> (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


      Comment

      • Rob Williscroft

        #4
        Re: static_cast usage

        Phlip wrote in news:N2Agb.3347 $lO1.77@newssvr 17.news.prodigy .com:
        [color=blue][color=green]
        >> cout << static_cast<dou ble> x/y<<endl;[/color]
        >
        > Try:
        >
        > cout << static_cast<dou ble> (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;[/color]

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

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

        Rob.
        --

        Comment

        • Gary Labowitz

          #5
          Re: static_cast usage

          "Rob Williscroft" <rtw@freenet.RE MOVE.co.uk> wrote in message
          news:Xns940D9F8 0AECF6ukcoREMOV Efreenetrtw@195 .129.110.204...[color=blue]
          > Phlip wrote in news:N2Agb.3347 $lO1.77@newssvr 17.news.prodigy .com:
          >[color=green][color=darkred]
          > >> cout << static_cast<dou ble> x/y<<endl;[/color]
          > >
          > > Try:
          > >
          > > cout << static_cast<dou ble> (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;[/color]
          >
          > Assuming you don't have a broken optimizer you can also go "cast free".
          >
          > cout << (x * 1.0 / y) << endl;[/color]

          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<dou ble>(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


          Comment

          • Christian Brechbühler

            #6
            Re: static_cast usage

            Gary Labowitz wrote:[color=blue]
            > This raises another issue: do you really prefer double(x) to (double)x?[/color]

            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

            Comment

            • Gary Labowitz

              #7
              Re: static_cast usage

              "Christian Brechbühler" <c_brechbuehler @yhaoo.com> wrote in message
              news:7XJgb.7074 41$uu5.116817@s ccrnsc04...[color=blue]
              > Gary Labowitz wrote:[color=green]
              > > This raises another issue: do you really prefer double(x) to (double)x?[/color]
              >
              > 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.[/color]

              Oh, oh. I hate to admit it on this one, but I'm the teacher!
              I ask them to use static cast.
              --
              Gary


              Comment

              • Phlip

                #8
                Re: static_cast usage

                > > > cout << static_cast<dou ble> (x)/y<<endl;
                [color=blue]
                > No wonder the archives search didn't find anything![/color]

                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

                Comment

                Working...