Is cast operator unary or binary? How many operands?

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

    Is cast operator unary or binary? How many operands?

    How may operators and operands does (typename) expression has?

    I'd say one operator, the cast operator, and two operands: typename
    and expression.

    But everywhere I read, cast is categorized as an unary operator. Why
    is that? Is it just a syntax cotegory?

    Thanks.

    José María.
  • vippstar@gmail.com

    #2
    Re: Is cast operator unary or binary? How many operands?

    On Apr 29, 3:45 pm, JoseMariaSola <JoseMariaS...@ gmail.comwrote:
    How may operators and operands does (typename) expression has?
    >
    I'd say one operator, the cast operator, and two operands: typename
    and expression.
    >
    But everywhere I read, cast is categorized as an unary operator. Why
    is that? Is it just a syntax cotegory?
    (typename)(expr ession) has one operator `(typename)' and one operand
    `(expression)'.
    The reason the type is enclosed in parentheses (as a design decision)
    is probably to avoid ambiguity, consider this:

    int i = 1; /* define and initialize i to 1 */
    {
    int i; /* cast i to int, a statement with no effect, or define i in
    block scope? */
    }

    Comment

    • JoseMariaSola

      #3
      Re: Is cast operator unary or binary? How many operands?

      I'd say one operator, the cast operator, and two operands: typename
      and expression.
      >
      But everywhere I read, cast is categorized as an unary operator. Why
      is that? Is it just a syntax cotegory?
      >
      (typename)(expr ession) has one operator `(typename)' and one operand
      `(expression)'.
      The reason the type is enclosed in parentheses (as a design decision)
      is probably to avoid ambiguity, consider this:
      >
      int i = 1; /* define and initialize i to 1 */
      {
      int i; /* cast i to int, a statement with no effect, or define i in
      block scope? */
      >
      }
      Thanks, Vipps.

      According to your answeer, the operator '(typename)' is very
      particular, because it not a single token but three AND the middle
      token is anything an identifier may be.

      JM.

      Comment

      • JoseMariaSola

        #4
        Re: Is cast operator unary or binary? How many operands?

        On Apr 29, 1:47 pm, JoseMariaSola <JoseMariaS...@ gmail.comwrote:
        I'd say one operator, the cast operator, and two operands: typename
        and expression.
        >
        But everywhere I read, cast is categorized as an unary operator. Why
        is that? Is it just a syntax cotegory?
        >
        (typename)(expr ession) has one operator `(typename)' and one operand
        `(expression)'.
        The reason the type is enclosed in parentheses (as a design decision)
        is probably to avoid ambiguity, consider this:
        >
        int i = 1; /* define and initialize i to 1 */
        {
        int i; /* cast i to int, a statement with no effect, or define i in
        block scope? */
        >
        }
        >
        Thanks, Vipps.
        >
        According to your answeer, the operator '(typename)' is very
        particular, because it not a single token but three AND the middle
        token is anything an identifier may be.
        >
        JM.
        The last line of my last post shoudl be:
        ... the middle token is anything an identifier may be and more.

        Here is part of the grammar:

        unary-expression:
        postfix-expression
        ++ unary-expression
        -- unary-expression
        unary-operator cast-expression
        sizeof unary-expression
        sizeof ( type-name )

        unary-operator: one of
        & * + - ˜ !

        cast-expression:
        unary-expression
        ( type-name ) cast-expression


        Why sizeof, (type-name), ++ and -- aren't unary-operators?

        Thanks.

        Comment

        • Richard Tobin

          #5
          Re: Is cast operator unary or binary? How many operands?

          In article <5c21f815-e1e6-468e-8630-cc72dad60a09@25 g2000hsx.google groups.com>,
          JoseMariaSola <JoseMariaSola@ gmail.comwrote:
          >Here is part of the grammar:
          >
          >unary-expression:
          postfix-expression
          ++ unary-expression
          -- unary-expression
          unary-operator cast-expression
          sizeof unary-expression
          sizeof ( type-name )
          >
          >unary-operator: one of
          & * + - ˜ !
          >
          >cast-expression:
          unary-expression
          ( type-name ) cast-expression
          >
          >
          >Why sizeof, (type-name), ++ and -- aren't unary-operators?
          You could include sizeof in unary-operator, but you'd still need the
          special case of a type-name operand requiring parentheses, and it
          seems clearer to keep them together in the list. Similarly ++ and --
          would still have to appear in postfix-expression. Both of these are
          just matters of taste really. I don't see how you could do
          (type-name), because a unary-operator is something that precedes its
          operand, and the parentheses of a cast have to go around the operand.

          -- Richard

          --
          :wq

          Comment

          • JoseMariaSola

            #6
            Re: Is cast operator unary or binary? How many operands?

            On Apr 29, 2:18 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
            In article <5c21f815-e1e6-468e-8630-cc72dad60...@25 g2000hsx.google groups.com>,
            >
            >
            >
            JoseMariaSola <JoseMariaS...@ gmail.comwrote:
            Here is part of the grammar:
            >
            unary-expression:
            postfix-expression
            ++ unary-expression
            -- unary-expression
            unary-operator cast-expression
            sizeof unary-expression
            sizeof ( type-name )
            >
            unary-operator: one of
            & * + - ˜ !
            >
            cast-expression:
            unary-expression
            ( type-name ) cast-expression
            >
            Why sizeof, (type-name), ++ and -- aren't unary-operators?
            >
            You could include sizeof in unary-operator, but you'd still need the
            special case of a type-name operand requiring parentheses, and it
            seems clearer to keep them together in the list. Similarly ++ and --
            would still have to appear in postfix-expression. Both of these are
            just matters of taste really.
            I don't see how you could do
            (type-name), because a unary-operator is something that precedes its
            operand, and the parentheses of a cast have to go around the operand.
            But in the case of cast the (only) operand is the expression the left,
            not the type-name.

            Talking with you both I notice that every operator requires
            expressions as operands, and type-name is not an expression. Am I
            right?

            JM.

            Comment

            • Keith Thompson

              #7
              Re: Is cast operator unary or binary? How many operands?

              JoseMariaSola <JoseMariaSola@ gmail.comwrites :
              [...]
              Talking with you both I notice that every operator requires
              expressions as operands, and type-name is not an expression. Am I
              right?
              Ideally, yes, but C is not an ideal language (not that I'm arguing it
              needs to be).

              "sizeof" is a unary operator; its operand can be either an expression
              (specifically a unary-expression) or a parenthesized type name.

              Or, if you prefer, "sizeof ( type-name )" is a special form of
              expression. You don't *have* to think of the parenthesized type name
              as an operand. But the standard puts it in the same section with the
              real unary operators, probably because the other form of "sizeof" is
              in that section. It would have been more logical, but less clear, to
              separate them.

              "." and "->" could be thought of as binary operators whose right
              operand is an identifier, but the standard treats them as postfix
              operators. Given:
              struct foo { int x; int y; };
              you can think of ".x" and ".y" as two distinct postfix operators, both
              applicable to operands of type "struct foo".

              --
              Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Richard Tobin

                #8
                Re: Is cast operator unary or binary? How many operands?

                In article <13115f63-fd53-4436-879d-4dab6f0fcd84@x3 5g2000hsb.googl egroups.com>,
                JoseMariaSola <JoseMariaSola@ gmail.comwrote:
                >But in the case of cast the (only) operand is the expression the left,
                >not the type-name.
                I see, you want to consider, say, (int) as an operator with a single
                operand, rather than (int)x containing an operator with two operands,
                "int" and "x".

                This would imply an infinite number of operators, which is not
                out of the question but doesn't seem to offer any advantage.
                >Talking with you both I notice that every operator requires
                >expressions as operands, and type-name is not an expression. Am I
                >right?
                The two traditional uses of "( type-name )" - in casts and sizeof -
                and the new use in C99 - in compound literals - are indeed
                exceptional. I previously suggested that we could factor out

                type-expression:
                ( type-name )

                which would make sizeof in particular more regular:

                unary-expression:
                ...
                sizeof unary-expression
                sizeof type-expression

                -- Richard
                --
                :wq

                Comment

                • JoseMariaSola

                  #9
                  Re: Is cast operator unary or binary? How many operands?

                  On Apr 29, 5:31 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                  In article <13115f63-fd53-4436-879d-4dab6f0fc...@x3 5g2000hsb.googl egroups.com>,
                  >
                  JoseMariaSola  <JoseMariaS...@ gmail.comwrote:
                  But in the case of cast the (only) operand is the expression the left,
                  not the type-name.
                  >
                  I see, you want to consider, say, (int) as an operator with a single
                  operand, rather than (int)x containing an operator with two operands,
                  "int" and "x".
                  >
                  This would imply an infinite number of operators, which is not
                  out of the question but doesn't seem to offer any advantage.
                  >
                  Talking with you both I notice that every operator requires
                  expressions as operands, and type-name is not an expression. Am I
                  right?
                  >
                  The two traditional uses of "( type-name )" - in casts and sizeof -
                  and the new use in C99 - in compound literals - are indeed
                  exceptional.  I previously suggested that we could factor out
                  >
                    type-expression:
                       ( type-name )
                  >
                  which would make sizeof in particular more regular:
                  >
                    unary-expression:
                       ...
                       sizeof unary-expression
                       sizeof type-expression
                  >
                  -- Richard
                  --
                  :wq
                  Thanks, Richard.

                  So both following expressions have one operator and one operand?
                  sizeof(int)
                  (int)x

                  typename is considered an operand in the firs expressin but not in the
                  second one?

                  JM.

                  Comment

                  • Richard Tobin

                    #10
                    Re: Is cast operator unary or binary? How many operands?

                    In article <f6c6e3fe-d076-43be-9cb8-4d29da011908@x3 5g2000hsb.googl egroups.com>,
                    JoseMariaSola <JoseMariaSola@ gmail.comwrote:
                    >But in the case of cast the (only) operand is the expression the left,
                    >not the type-name.
                    [...]
                    >So both following expressions have one operator and one operand?
                    sizeof(int)
                    (int)x
                    *I* dont' consider it that way. I was just exploring the consequences
                    of your suggestion. I would say that they have one and two operands
                    respectively.

                    -- Richard
                    --
                    :wq

                    Comment

                    • Chris Torek

                      #11
                      Re: Is cast operator unary or binary? How many operands?

                      In article <f6c6e3fe-d076-43be-9cb8-4d29da011908@x3 5g2000hsb.googl egroups.com>
                      JoseMariaSola <JoseMariaSola@ gmail.comwrote:
                      >So both following expressions have one operator and one operand?
                      sizeof(int)
                      (int)x
                      Yes.
                      >typename is considered an operand in the firs expressin but not in the
                      >second one?
                      Yes. (In the second one, the type name is part of the operator.)

                      This is just a matter of terminology, in this case, as applied to
                      parsing. Since the terminology is made up by humans, it need not
                      be entirely consistent, or even make all that much sense, as long
                      as it lets one human communicate with another. :-)

                      The way to view this, to make sense out of it, is that the cast
                      operator is inherently made up of the token sequence '(',
                      type-name-keyword [%], ')'. Having consumed the type-name by this
                      point, the only "argument" left to call an "operand" is the value
                      being cast. The sizeof operator is inherently made up of the
                      keyword token 'sizeof', and if its operand is a type-name, the
                      type-name is syntactically required to have outer-level parentheses.
                      Since the parentheses are (again) consumed by the syntax, the
                      "argument" that is left, that can be called an "operand", is the
                      type-name (without, in this case, the parentheses, although it
                      makes no real difference anyway).

                      [% In C as originally defined by Dennis Ritchie, "typedef" did not
                      even exist. So all type names were keywords, which made parsing
                      C easy. All declarations were instantly obvious, as they always
                      started with a keyword, and since the total set of keywords was
                      small, everyone knew all of them at a glance. When typedef was
                      later added, some time in the late 1970s, the language became much
                      more complicated syntactically, since typedef identifiers are only
                      distinguishable from other identifiers by context. For instance,
                      the C89 -- but not C99 -- code fragment:

                      void f(x);

                      can mean either:

                      - declare f as function taking one argument of type int, and
                      returning void; or

                      - declare f as function taking one argument of type x, and
                      returning void.

                      The former makes use of C89's "implicit int"; the latter occurs if
                      and only if "x" is a typedef-name. C99 removes this particular
                      ambiguity by outlawing "implicit int", but other difficult parsing
                      situations remain. From the parser's point of view, typedef-ed
                      identifiers are "just like" keywords, except when they are not.
                      If one attempts to use a conventional scanner and parser generator
                      like (f)lex and (b)yacc, one must invent clever feedback and/or
                      feed-forward mechanisms by which the parser can tell the lexer
                      whether to claim a potential typedef name is a "type name" or an
                      "identifier name". (Or one could use a more powerful parsing
                      technique, but that means abandoning yacc.)

                      Because it is impossible to tell at a glance whether some identifier
                      is actually a typedef-name, C programmers often find themselves
                      using some sort of self-imposed (or company coding standard imposed)
                      typographic convention to "mark" all typedefs. For instance, some
                      people use _t suffixes, so that it is clear that greeble_t is a
                      type-name. Others use an initial capital letter, or all-uppercase.]
                      --
                      In-Real-Life: Chris Torek, Wind River Systems
                      Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                      email: gmail (figure it out) http://web.torek.net/torek/index.html

                      Comment

                      • jacob navia

                        #12
                        Re: Is cast operator unary or binary? How many operands?

                        JoseMariaSola wrote:
                        How may operators and operands does (typename) expression has?
                        >
                        I'd say one operator, the cast operator, and two operands: typename
                        and expression.
                        >
                        But everywhere I read, cast is categorized as an unary operator. Why
                        is that? Is it just a syntax cotegory?
                        >
                        Thanks.
                        >
                        José María.
                        It has one argument: the expression, and a *result*
                        of type "typename"

                        When I write

                        (double)i;

                        I am conceptually calling

                        double TransformIntege rToDouble(int);

                        a function that takes ONE integer argument and outputs
                        a double number. Obviously there is no such a function
                        since the compiler inlines this cast when compiling, but
                        in some cases an actual function call will be generated.



                        --
                        jacob navia
                        jacob at jacob point remcomp point fr
                        logiciels/informatique

                        Comment

                        • Richard Tobin

                          #13
                          Re: Is cast operator unary or binary? How many operands?

                          In article <fv9r4v0248d@ne ws3.newsguy.com >,
                          Chris Torek <nospam@torek.n etwrote:
                          >The way to view this, to make sense out of it, is that the cast
                          >operator is inherently made up of the token sequence '(',
                          >type-name-keyword [%], ')'.
                          If you take this view, it makes more sense to say "... cast operators
                          are inherently made up of ...", since even if (int) is an operator,
                          it's clearly not the same operator as (double). The C standard seems
                          to do this except in the index, where "cast operator, ( )" points to
                          the section entitled "Cast operators".

                          -- Richard
                          --
                          :wq

                          Comment

                          • Richard Tobin

                            #14
                            Re: Is cast operator unary or binary? How many operands?

                            In article <fv9s2t$mu7$1@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
                            >When I write
                            >
                            >(double)i;
                            >
                            >I am conceptually calling
                            >
                            >double TransformIntege rToDouble(int);
                            Why are you not conceptually calling Transform(int, double)?

                            -- Richard
                            --
                            :wq

                            Comment

                            • jacob navia

                              #15
                              Re: Is cast operator unary or binary? How many operands?

                              Richard Tobin wrote:
                              In article <fv9s2t$mu7$1@a ioe.org>, jacob navia <jacob@nospam.o rgwrote:
                              >
                              >When I write
                              >>
                              >(double)i;
                              >>
                              >I am conceptually calling
                              >>
                              >double TransformIntege rToDouble(int);
                              >
                              Why are you not conceptually calling Transform(int, double)?
                              >
                              -- Richard
                              Because the "int" is the RESULT type, not the input type!

                              Besides, a hyper hyper generic procedure:
                              Transform(anyth ing,anything)
                              is just a dream...

                              You can have only concrete procedures:

                              Something TransformToSome thing(something _else);

                              TransformIntege rToDouble exists (at least in lcc-win),
                              and will take care of the details, correct rounding, etc etc.

                              Transform(int,d ouble) doesn't exist and can't exist since
                              it would have to take ANY two input types and transform the
                              second into the first, an impossible task!


                              --
                              jacob navia
                              jacob at jacob point remcomp point fr
                              logiciels/informatique

                              Comment

                              Working...