static_cast vs. C-stype cast

Discussion in 'C++' started by Alexander Stippler, Dec 8, 2003.

  1. In some situations I can apply a C-style cast,
    but no static_cast. I'm not quite sure about the
    differences in such places. A little example:

    template <typename T>
    class Dummy {
    public:
    Dummy(int i) {}
    };

    template <typename T>
    void dummy(const Dummy<T> &a) {}

    int
    main()
    {
    int i=6;
    // a traditional cast works ...
    dummy((const Dummy<int> &)i);

    // but the static_cast is an invalid type conversion.
    dummy(static_cast<const Dummy<int> &>(i));
    return 0;
    }


    I know, that a static_cast to Dummy<int> would have worked in the second
    call, but what's the difference between the two casts in the main() above?

    regards,
    alex
     
    Alexander Stippler, Dec 8, 2003
    #1
    1. Advertising

  2. "Alexander Stippler" <-ulm.de> wrote in message
    news:-ulm.de...

    [snip]

    | int
    | main()
    | {
    | int i=6;
    | // a traditional cast works ...
    | dummy((const Dummy<int> &)i);
    |
    | // but the static_cast is an invalid type conversion.
    | dummy(static_cast<const Dummy<int> &>(i));
    | return 0;
    | }
    |
    |
    | I know, that a static_cast to Dummy<int> would have worked in the second
    | call, but what's the difference between the two casts in the main() above?

    Are you sure you didn't want the following instead ?:

    dummy( ( const Dummy<int> ) i );
    dummy( static_cast<const Dummy<int> >( i ) );

    Cheers.
    Chris Val
     
    Chris \( Val \), Dec 9, 2003
    #2
    1. Advertising

  3. Alexander Stippler <-ulm.de> wrote:
    > In some situations I can apply a C-style cast,
    > but no static_cast. I'm not quite sure about the
    > differences in such places. A little example:
    >
    > template <typename T>
    > class Dummy {
    > public:
    > Dummy(int i) {}
    > };
    >
    > template <typename T>
    > void dummy(const Dummy<T> &a) {}
    >
    > int
    > main()
    > {
    > int i=6;
    > // a traditional cast works ...
    > dummy((const Dummy<int> &)i);
    >
    > // but the static_cast is an invalid type conversion.
    > dummy(static_cast<const Dummy<int> &>(i));
    > return 0;
    > }
    >
    >
    > I know, that a static_cast to Dummy<int> would have worked in the second
    > call, but what's the difference between the two casts in the main() above?


    I'm not sure. Maybe it's considered to be
    two casts: 'int' ==> 'Dummy' and then
    casting the temporary to a const reference?
    BTW, what's so bad about
    dummy( Dummy<int>(i) )
    ? I think this
    dummy<int>( i );

    should work as well.

    > regards,
    > alex



    Schobi

    --
    is never read
    I'm Schobi at suespammers dot org

    "Sometimes compilers are so much more reasonable than people."
    Scott Meyers
     
    Hendrik Schober, Dec 10, 2003
    #3
    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. Dave Rahardja

    static_cast versus C-style type cast

    Dave Rahardja, Aug 18, 2003, in forum: C++
    Replies:
    1
    Views:
    543
    Rolf Magnus
    Aug 18, 2003
  2. Kobe
    Replies:
    3
    Views:
    618
    Tomás
    Feb 15, 2006
  3. Bo Peng
    Replies:
    11
    Views:
    1,113
    Victor Bazarov
    Oct 20, 2006
  4. Vincent RICHOMME

    static_cast better than C cast ?

    Vincent RICHOMME, May 2, 2007, in forum: C++
    Replies:
    9
    Views:
    401
    James Kanze
    May 3, 2007
  5. junyangzou
    Replies:
    13
    Views:
    278
Loading...

Share This Page