conversion syntax

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

    conversion syntax

    It it permissible to use the constructor style cast with primitives
    such as "unsigned long"? One of my compilers accepts this syntax, the
    other does not. The failing one chokes on the fact that the type is
    not a single identifier or keyword. Which one is correct? For example:

    unsigned long x = unsigned long(y);



    REH


  • Juha Nieminen

    #2
    Re: conversion syntax

    REH wrote:
    It it permissible to use the constructor style cast with primitives
    such as "unsigned long"? One of my compilers accepts this syntax, the
    other does not. The failing one chokes on the fact that the type is
    not a single identifier or keyword. Which one is correct? For example:
    >
    unsigned long x = unsigned long(y);
    If I'm not mistaken, if you want to make a C-style cast to a type
    which consists of several keywords, you have to enclose them in
    parentheses. That is:

    unsigned long x = (unsigned long)(y);

    Of course the recommended way in C++ is to use a C++ style cast:

    unsigned long x = static_cast<uns igned long>(y);

    Comment

    • REH

      #3
      Re: conversion syntax

      On Oct 8, 10:36 am, Juha Nieminen <nos...@thanks. invalidwrote:
      REH wrote:
      It it permissible to use the constructor style cast with primitives
      such as "unsigned long"? One of my compilers accepts this syntax, the
      other does not. The failing one chokes on the fact that the type is
      not a single identifier or keyword. Which one is correct? For example:
      >
      unsigned long x = unsigned long(y);
      >
        If I'm not mistaken, if you want to make a C-style cast to a type
      which consists of several keywords, you have to enclose them in
      parentheses. That is:
      >
          unsigned long x = (unsigned long)(y);
      >
        Of course the recommended way in C++ is to use a C++ style cast:
      >
          unsigned long x = static_cast<uns igned long>(y);
      Thanks. I know. I was just wondering if the constructor style was
      legal.

      Comment

      • Maxim Yegorushkin

        #4
        Re: conversion syntax

        On Oct 8, 3:36 pm, Juha Nieminen <nos...@thanks. invalidwrote:
        REH wrote:
        It it permissible to use the constructor style cast with primitives
        such as "unsigned long"? One of my compilers accepts this syntax, the
        other does not. The failing one chokes on the fact that the type is
        not a single identifier or keyword. Which one is correct? For example:
        >
        unsigned long x = unsigned long(y);
        >
          If I'm not mistaken, if you want to make a C-style cast to a type
        which consists of several keywords, you have to enclose them in
        parentheses. That is:
        >
            unsigned long x = (unsigned long)(y);
        >
          Of course the recommended way in C++ is to use a C++ style cast:
        >
            unsigned long x = static_cast<uns igned long>(y);
        The original question, IMO, involved C++ functional style cast, rather
        than C-style cast.

        --
        Max

        Comment

        • Juha Nieminen

          #5
          Re: conversion syntax

          REH wrote:
          Thanks. I know. I was just wondering if the constructor style was
          legal.
          Well, it is legal as long as your type consists of one single symbol.
          For example this will always be legal:

          template<typena me T>
          void foo()
          {
          T x = T(y);
          }

          This even if you call it like foo<unsigned long>(). That's because 'T'
          is just one symbol.

          Rather curiously, this is legal too:

          template<typena me T>
          void foo()
          {
          T x = (T)y;
          }

          And this even if T is a class (with a constructor which takes a
          parameter of the type of 'y').

          Comment

          • Pascal J. Bourguignon

            #6
            Re: conversion syntax

            REH <spamjunk@stny. rr.comwrites:
            It it permissible to use the constructor style cast with primitives
            such as "unsigned long"? One of my compilers accepts this syntax, the
            other does not. The failing one chokes on the fact that the type is
            not a single identifier or keyword. Which one is correct? For example:
            >
            unsigned long x = unsigned long(y);
            Well, C++ syntax is rather complex. It's understandable it's hard to
            read, to understand and therefore to implement correctly. I don't
            know what is specified there.

            Since you observe that's a hard dark corner with diverging
            implementation, my advice would be to avoid it. Or to use parentheses.

            typedef unsigned long ulong;
            ulong x = ulong(y);

            or:

            unsigned long x = (unsigned long)(y);


            --
            __Pascal Bourguignon__

            Comment

            • REH

              #7
              Re: conversion syntax

              On Oct 8, 10:47 am, p...@informatim ago.com (Pascal J. Bourguignon)
              wrote:
              REH <spamj...@stny. rr.comwrites:
              It it permissible to use the constructor style cast with primitives
              such as "unsigned long"? One of my compilers accepts this syntax, the
              other does not. The failing one chokes on the fact that the type is
              not a single identifier or keyword. Which one is correct? For example:
              >
              unsigned long x = unsigned long(y);
              >
              Well, C++ syntax is rather complex.  It's understandable it's hard to
              read, to understand and therefore to implement correctly.   I don't
              know what is specified there.
              >
              Since you observe that's a hard dark corner with diverging
              implementation, my advice would be to avoid it.  Or to use parentheses.
              >
                  typedef unsigned long ulong;
                  ulong x = ulong(y);
              >
              or:
              >
                  unsigned long x = (unsigned long)(y);
              >
              --
              __Pascal Bourguignon__
              Sigh. Yes, I know that. My question is simply: is the syntax legal or
              not?

              REH

              Comment

              • Pascal J. Bourguignon

                #8
                Re: conversion syntax

                Maxim Yegorushkin <maxim.yegorush kin@gmail.comwr ites:
                On Oct 8, 3:36 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                >REH wrote:
                It it permissible to use the constructor style cast with primitives
                such as "unsigned long"? One of my compilers accepts this syntax, the
                other does not. The failing one chokes on the fact that the type is
                not a single identifier or keyword. Which one is correct? For example:
                >>
                unsigned long x = unsigned long(y);
                >>
                >  If I'm not mistaken, if you want to make a C-style cast to a type
                >which consists of several keywords, you have to enclose them in
                >parentheses. That is:
                >>
                >    unsigned long x = (unsigned long)(y);
                >>
                >  Of course the recommended way in C++ is to use a C++ style cast:
                >>
                >    unsigned long x = static_cast<uns igned long>(y);
                >
                The original question, IMO, involved C++ functional style cast, rather
                than C-style cast.
                Which funnily, is not decided when you write:

                unsigned long x = (unsigned long)(y);

                The compiler could as well decide to parse it as a C cast as a C++
                functional style cast. I think it'll be my favorite way to write it,
                let the compiler deal with it as it wants :-)

                --
                __Pascal Bourguignon__

                Comment

                • REH

                  #9
                  Re: conversion syntax

                  On Oct 8, 11:29 am, p...@informatim ago.com (Pascal J. Bourguignon)
                  wrote:
                  Maxim Yegorushkin <maxim.yegorush ...@gmail.comwr ites:
                  On Oct 8, 3:36 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                  REH wrote:
                  It it permissible to use the constructor style cast with primitives
                  such as "unsigned long"? One of my compilers accepts this syntax, the
                  other does not. The failing one chokes on the fact that the type is
                  not a single identifier or keyword. Which one is correct? For example:
                  >
                  unsigned long x = unsigned long(y);
                  >
                    If I'm not mistaken, if you want to make a C-style cast to a type
                  which consists of several keywords, you have to enclose them in
                  parentheses. That is:
                  >
                      unsigned long x = (unsigned long)(y);
                  >
                    Of course the recommended way in C++ is to use a C++ style cast:
                  >
                      unsigned long x = static_cast<uns igned long>(y);
                  >
                  The original question, IMO, involved C++ functional style cast, rather
                  than C-style cast.
                  >
                  Which funnily, is not decided when you write:
                  >
                      unsigned long x = (unsigned long)(y);
                  >
                  The compiler could as well decide to parse it as a C cast as a C++
                  functional style cast.   I think it'll be my favorite way to write it,
                  let the compiler deal with it as it wants :-)
                  >
                  Maxim understands that it was a question about syntax, not semantics.

                  REH

                  Comment

                  • REH

                    #10
                    Re: conversion syntax

                    On Oct 8, 10:29 am, REH <spamj...@stny. rr.comwrote:
                    It it permissible to use the constructor style cast with primitives
                    such as "unsigned long"? One of my compilers accepts this syntax, the
                    other does not. The failing one chokes on the fact that the type is
                    not a single identifier or keyword. Which one is correct? For example:
                    >
                    unsigned long x = unsigned long(y);
                    >
                    OK, if I read the sections in the standard defining "simple-type-
                    specifier" and "postfix-expression" correctly, the syntax is not
                    valid.

                    REH

                    Comment

                    • REH

                      #11
                      Re: conversion syntax

                      On Oct 8, 11:29 am, p...@informatim ago.com (Pascal J. Bourguignon)
                      wrote:
                      Which funnily, is not decided when you write:
                      >
                          unsigned long x = (unsigned long)(y);
                      >
                      The compiler could as well decide to parse it as a C cast as a C++
                      functional style cast.   I think it'll be my favorite way to write it,
                      let the compiler deal with it as it wants :-)
                      >
                      It don't matter how you write it, when there is a valid conversion
                      from T to U, these are all equivalent:

                      U(T);
                      (U) T;
                      static_cast<U>( T);


                      REH

                      Comment

                      • Pete Becker

                        #12
                        Re: conversion syntax

                        On 2008-10-08 12:38:20 -0400, REH <spamjunk@stny. rr.comsaid:
                        On Oct 8, 11:29 am, p...@informatim ago.com (Pascal J. Bourguignon)
                        wrote:
                        >Which funnily, is not decided when you write:
                        >>
                        >    unsigned long x = (unsigned long)(y);
                        >>
                        >The compiler could as well decide to parse it as a C cast as a C++
                        >functional style cast.   I think it'll be my favorite way to write it,
                        >let the compiler deal with it as it wants :-)
                        >>
                        >
                        It don't matter how you write it, when there is a valid conversion
                        from T to U, these are all equivalent:
                        >
                        U(T);
                        (U) T;
                        static_cast<U>( T);
                        >

                        It does matter what you write. The second also applies to situations
                        where the others don't, and some people think that this leads to errors.

                        --
                        Pete
                        Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                        Standard C++ Library Extensions: a Tutorial and Reference
                        (www.petebecker.com/tr1book)

                        Comment

                        • REH

                          #13
                          Re: conversion syntax

                          On Oct 8, 12:41 pm, Pete Becker <p...@versatile coding.comwrote :
                          On 2008-10-08 12:38:20 -0400, REH <spamj...@stny. rr.comsaid:
                          >
                          >
                          >
                          On Oct 8, 11:29 am, p...@informatim ago.com (Pascal J. Bourguignon)
                          wrote:
                          Which funnily, is not decided when you write:
                          >
                              unsigned long x = (unsigned long)(y);
                          >
                          The compiler could as well decide to parse it as a C cast as a C++
                          functional style cast.   I think it'll be my favorite way to write it,
                          let the compiler deal with it as it wants :-)
                          >
                          It don't matter how you write it, when there is a valid conversion
                          from T to U, these are all equivalent:
                          >
                          U(T);
                          (U) T;
                          static_cast<U>( T);
                          >
                          It does matter what you write. The second also applies to situations
                          where the others don't, and some people think that this leads to errors.
                          >
                          I use "doesn't matter" to mean when the three versions valid, change
                          from one to the other will not produce a different meaning. I agree
                          that c-style casts can be dangerous.

                          REH

                          Comment

                          • James Kanze

                            #14
                            Re: conversion syntax

                            On Oct 8, 5:29 pm, p...@informatim ago.com (Pascal J. Bourguignon)
                            wrote:
                            Maxim Yegorushkin <maxim.yegorush ...@gmail.comwr ites:
                            On Oct 8, 3:36 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                            REH wrote:
                            It it permissible to use the constructor style cast with
                            primitives such as "unsigned long"? One of my compilers
                            accepts this syntax, the other does not. The failing one
                            chokes on the fact that the type is not a single
                            identifier or keyword. Which one is correct? For example:
                            unsigned long x = unsigned long(y);
                            If I'm not mistaken, if you want to make a C-style cast to
                            a type which consists of several keywords, you have to
                            enclose them in parentheses.
                            Not a C-style cast; an "explicit type conversion (functional
                            notation)". The sytax for this requires either a
                            simple-type-specifier or a typename-specifier to the left of the
                            parentheses. A simple-type-specifier can be either a single
                            token, or a qualified type name, e.g. X::Y::Z. It cannot be the
                            name of a type which requires several tokens to specify, so the
                            answer to his question is no.
                            That is:
                            unsigned long x = (unsigned long)(y);
                            Of course the recommended way in C++ is to use a C++ style cast:
                            unsigned long x = static_cast<uns igned long>(y);
                            Recommended by whom? I've never seen code which didn't use the
                            functional notation for type conversion; if the number of
                            arguments is anything other than one, it's the only type of type
                            conversion which works. I'd strongly recommend using the new
                            cast syntax for pointer and reference casts, but for the others,
                            I generally use either a C style cast or the functional
                            notation.
                            The original question, IMO, involved C++ functional style
                            cast, rather than C-style cast.
                            Which funnily, is not decided when you write:
                            unsigned long x = (unsigned long)(y);
                            Yes it is. That's an "explicit type conversion (cast
                            notation)"---a C style cast. The functional notation has a
                            particularity that the others don't have: you can convert
                            nothing, or more than one thing:-). (And yes, calling it a
                            conversion is a bit strange, but that's what the standard does.)
                            So given a user defined type A:

                            A() explicit type conversion (functional notation)
                            (A)() illegal
                            static_cast< A >() illegal

                            A( x ) explicit type conversion (functional notation)
                            (A)( x ) explicit type conversion (cast notation)
                            static_cast< A >( x ) static_cast

                            A( x, y ) explicit type conversion (functional notation)
                            (A)( x, y ) illegal
                            static_cast< A >( x, y ) illegal

                            In each group of three, all of the legal syntaxes have exactly
                            the same semantics (and note that A(x) doesn't necessarily call
                            the constructor of A---it can also call a user defined coversion
                            operator of x).
                            The compiler could as well decide to parse it as a C cast as a
                            C++ functional style cast. I think it'll be my favorite way
                            to write it, let the compiler deal with it as it wants :-)
                            Personally, I think it would have been clearer if the cast
                            notation had been extended to allow multiple arguments, and the
                            function style casts hadn't been introduced. That would have
                            eliminated a lot of ambiguities. But it's too late for that
                            now.

                            --
                            James Kanze (GABI Software) email:james.kan ze@gmail.com
                            Conseils en informatique orientée objet/
                            Beratung in objektorientier ter Datenverarbeitu ng
                            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                            Comment

                            • REH

                              #15
                              Re: conversion syntax

                              On Oct 8, 12:51 pm, James Kanze <james.ka...@gm ail.comwrote:
                              Not a C-style cast; an "explicit type conversion (functional
                              notation)".  The sytax for this requires either a
                              simple-type-specifier or a typename-specifier to the left of the
                              parentheses.  A simple-type-specifier can be either a single
                              token, or a qualified type name, e.g. X::Y::Z.  It cannot be the
                              name of a type which requires several tokens to specify, so the
                              answer to his question is no.
                              >
                              Thank you, Mr. Kanze.

                              REH

                              Comment

                              Working...