Avoiding Implicit Casting

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

    Avoiding Implicit Casting

    Hi All,

    Is it possible to disallow implicit casting for an operand of a function
    written in C?

    i.e.
    void foo(int a) {..}
    short b;
    foo(b) // error without explicit cast

    Thanks,

    Simon ;o)


  • Dave Vandervies

    #2
    Re: Avoiding Implicit Casting

    In article <blkl15$dja$1@t itan.btinternet .com>,
    Simon <sorrynomail@no .no> wrote:[color=blue]
    >Hi All,
    >
    >Is it possible to disallow implicit casting for an operand of a function
    >written in C?[/color]

    Terminology nitpick: There's no such thing as an implicit cast; a
    cast is a source-level construct (which forces a coversion) and is by
    definition explicit.
    The term you're looking for is "implicit conversion".
    [color=blue]
    >i.e.
    >void foo(int a) {..}
    >short b;
    >foo(b) // error without explicit cast[/color]

    No, there's no way to avoid implicit conversions other than not writing
    code that uses them in the first place.


    The canonical question to ask at this point is "What are you really
    trying to do?".


    dave

    --
    Dave Vandervies dj3vande@csclub .uwaterloo.ca
    The n869 draft does not define the language, but this non-definition is more
    authoritative (though less readable) than K&R's non-definition of the language.
    --Richard Heathfield in comp.lang.c

    Comment

    • Jeremy Yallop

      #3
      Re: Avoiding Implicit Casting

      Simon wrote:[color=blue]
      > Is it possible to disallow implicit casting for an operand of a function
      > written in C?
      >
      > i.e.
      > void foo(int a) {..}
      > short b;
      > foo(b) // error without explicit cast[/color]

      Not really. (Why would you want to?) I can think of a couple of ways
      to achieve a similar effect:

      * use user-defined types (struct or union) instead of builtins.

      typedef struct { int value; } Int;
      typedef struct { short value; } Short;
      void foo(Int);
      Short b;
      foo(b); /* error */

      * pass pointers instead of values around.

      void foo(const int *);
      short b;
      foo(&b); /* error */

      These are likely to be fairly cumbersome to use, though.

      (As a side note, I wouldn't describe C's various implicit conversions
      as "casting". "Casting" always refers, I think, to conversions which
      the programmer explicitly requests by using a cast operator.)

      Jeremy.

      Comment

      • Joe Wright

        #4
        Re: Avoiding Implicit Casting

        Simon wrote:[color=blue]
        >
        > Hi All,
        >
        > Is it possible to disallow implicit casting for an operand of a function
        > written in C?
        >
        > i.e.
        > void foo(int a) {..}
        > short b;
        > foo(b) // error without explicit cast
        >[/color]
        #include <stdio.h>

        void foo(int a) {
        printf("%d\n", a);
        }

        int main(void) {
        short b = 42;
        foo(b);
        return 0;
        }
        Works fine. What am I missing?
        --
        Joe Wright http://www.jw-wright.com
        "Everything should be made as simple as possible, but not simpler."
        --- Albert Einstein ---

        Comment

        • Richard Heathfield

          #5
          Re: Avoiding Implicit Casting

          Joe Wright wrote:
          [color=blue]
          > Simon wrote:[color=green]
          >>
          >> Hi All,
          >>
          >> Is it possible to disallow implicit casting for an operand of a function
          >> written in C?
          >>
          >> i.e.
          >> void foo(int a) {..}
          >> short b;
          >> foo(b) // error without explicit cast
          >>[/color]
          > #include <stdio.h>
          >
          > void foo(int a) {
          > printf("%d\n", a);
          > }
          >
          > int main(void) {
          > short b = 42;
          > foo(b);
          > return 0;
          > }
          > Works fine. What am I missing?[/color]

          Only the fact that he wants your code to generate a diagnostic (or, perhaps,
          a run-time error).


          --
          Richard Heathfield : binary@eton.pow ernet.co.uk
          "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
          C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
          K&R answers, C books, etc: http://users.powernet.co.uk/eton

          Comment

          • Richard Heathfield

            #6
            Re: Avoiding Implicit Casting

            Simon wrote:
            [color=blue]
            > Hi All,
            >
            > Is it possible to disallow implicit casting for an operand of a function
            > written in C?[/color]

            Yes. I hereby disallow it. (There is no such thing as implicit casting in C.
            A cast is an explicit conversion, so "implicit cast" is an oxymoron.)
            [color=blue]
            > i.e.
            > void foo(int a) {..}
            > short b;
            > foo(b) // error without explicit cast[/color]

            Disabling normal conversion between short int and int would be short-sighted
            (but not int-sighted), since it would break a lot of existing code. *Why*
            do you want to do this? Cui bono?

            --
            Richard Heathfield : binary@eton.pow ernet.co.uk
            "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
            C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
            K&R answers, C books, etc: http://users.powernet.co.uk/eton

            Comment

            • Joe Wright

              #7
              Re: Avoiding Implicit Casting

              Richard Heathfield wrote:[color=blue]
              >
              > Joe Wright wrote:
              >[color=green]
              > > Simon wrote:[color=darkred]
              > >>
              > >> Hi All,
              > >>
              > >> Is it possible to disallow implicit casting for an operand of a function
              > >> written in C?
              > >>
              > >> i.e.
              > >> void foo(int a) {..}
              > >> short b;
              > >> foo(b) // error without explicit cast
              > >>[/color]
              > > #include <stdio.h>
              > >
              > > void foo(int a) {
              > > printf("%d\n", a);
              > > }
              > >
              > > int main(void) {
              > > short b = 42;
              > > foo(b);
              > > return 0;
              > > }
              > > Works fine. What am I missing?[/color]
              >
              > Only the fact that he wants your code to generate a diagnostic (or, perhaps,
              > a run-time error).
              >[/color]
              Do you still have "Reading for Comprehension" available? :-)
              --
              Joe Wright http://www.jw-wright.com
              "Everything should be made as simple as possible, but not simpler."
              --- Albert Einstein ---

              Comment

              • Dan Pop

                #8
                Re: Avoiding Implicit Casting

                In <blkl15$dja$1@t itan.btinternet .com> "Simon" <sorrynomail@no .no> writes:
                [color=blue]
                >Is it possible to disallow implicit casting for an operand of a function
                >written in C?
                >
                >i.e.
                >void foo(int a) {..}
                >short b;
                >foo(b) // error without explicit cast[/color]

                These implicit function argument conversions are a standard feature of the
                language, no conforming compiler is allowed not to perform them.

                At best, your compiler may have an option to warn when such conversions
                are performed. However, compilers usually can be made to warn about
                implicit conversions to narrower types, not to wider types (the latter
                being always value-preserving and, therefore, not a source of problems).

                You can also usually get warnings about implicit conversions to types
                of a different signedness (e.g. from short int to unsinged int).

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Kris Wempa

                  #9
                  Re: Avoiding Implicit Casting

                  You can force these to be errors by creating your own data type (struct or
                  union) and "wrapping" your value in the data type. I don't know how viable
                  that is for you, but it's a solution. Depending on your compiler, you may
                  also have the option to treat warnings like errors. You may want to look
                  into that as well.


                  "Dan Pop" <Dan.Pop@cern.c h> wrote in message
                  news:blrtfv$lib $7@sunnews.cern .ch...[color=blue]
                  > In <blkl15$dja$1@t itan.btinternet .com> "Simon" <sorrynomail@no .no> writes:
                  >[color=green]
                  > >Is it possible to disallow implicit casting for an operand of a function
                  > >written in C?
                  > >
                  > >i.e.
                  > >void foo(int a) {..}
                  > >short b;
                  > >foo(b) // error without explicit cast[/color]
                  >
                  > These implicit function argument conversions are a standard feature of the
                  > language, no conforming compiler is allowed not to perform them.
                  >
                  > At best, your compiler may have an option to warn when such conversions
                  > are performed. However, compilers usually can be made to warn about
                  > implicit conversions to narrower types, not to wider types (the latter
                  > being always value-preserving and, therefore, not a source of problems).
                  >
                  > You can also usually get warnings about implicit conversions to types
                  > of a different signedness (e.g. from short int to unsinged int).
                  >
                  > Dan
                  > --
                  > Dan Pop
                  > DESY Zeuthen, RZ group
                  > Email: Dan.Pop@ifh.de[/color]


                  Comment

                  • Micah Cowan

                    #10
                    Re: Avoiding Implicit Casting

                    "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> writes:
                    [color=blue]
                    > You can force these to be errors by creating your own data type (struct or
                    > union) and "wrapping" your value in the data type. I don't know how viable
                    > that is for you, but it's a solution. Depending on your compiler, you may
                    > also have the option to treat warnings like errors. You may want to look
                    > into that as well.[/color]

                    (Please don't top-post. Luckily, I didn't need any of what Dan
                    said for my response to you).

                    In addition to being ugly, this solution also only achieves an
                    extra level of indirection; you're still able to do:

                    struct foo { short bar; } quux;

                    int quuux = 10;
                    quux.bar = quuux;

                    -Micah

                    Comment

                    Working...