Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #24800

Re: Function factories

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Function factories
Date 2012-08-23 14:51 -0700
Organization None to speak of
Message-ID <ln8vd5gtts.fsf@nuthaus.mib.org> (permalink)
References (6 earlier) <k15vsk$vqr$4@solani.org> <50368620.8010204@verizon.net> <k162la$vqr$7@solani.org> <503690A7.6030404@verizon.net> <k1642l$1rd$2@news.albasani.net>

Show all headers | View raw


Melzzzzz <mel@zzzzz.com> writes:
[...]
> Well compiler warnings are enough aren't they? Cast just masks warning
> and makes compiler happy which is not good thing.
> Well if you have to search trough code to find that one error is much
> more difficult rather then just check compiler warning.
> I don;t like to shut up compiler warnings with casts and that's
> why C's approach is better.

The code in question was:

    double d;
    int *pi = &d;

and the question is whether a compiler should reject it with an error
message or accept it with a mere warning.

Personally, I would never deliberately write that.  As far as the C
standard is concerned, it's a constraint violation, requiring a
diagnostic from any conforming compiler.  If a compiler issues a warning
and continues to compile the code, it might seem reasonable to assume
that it will generate a conversion from `double*` to `int*` -- but in
fact the language standard does not say or imply that that's what will
happen.  The behavior is undefined; a compiler could legally generate
code that sets `pi` to a null pointer, for example.

Whereas if I *want* to do that conversion, there's a perfectly valid way
to do it that likely won't result in a warning:

    double d;
    int *pi = (int*)&d;

A problem I have with gcc is that it doesn't clearly distinguish between
warnings about constraint violations, like the above, and warnings about
legal but questionable code, such as writing `if (x = 42)` rather than
`if (x == 42)`.  Again, this doesn't violate the C standard; I just
personally find it annoying.

That's just my opinion (that all syntax errors and constraint violations
should be treated as fatal errors, not as warnings -- or at least that
there should be an option that works that way).  What's yours?  Which
errors do you think should be fatal, and which should merely trigger a
warning?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-21 20:36 +0000
  Re: Function factories Nobody <nobody@nowhere.com> - 2012-08-21 21:55 +0100
    Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 16:36 +0000
      Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 13:01 -0400
        Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 18:33 +0000
        Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 22:26 -0400
  Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 08:34 +0200
    Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-22 01:52 -0700
      Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 11:34 +0200
      Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:30 +0100
    Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:29 +0100
  Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-22 12:32 +0200
    Re: Function factories tom st denis <tom@iahu.ca> - 2012-08-22 07:40 -0700
      Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 11:05 -0400
        Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 11:08 +0200
        Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 13:27 +0100
          Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 11:13 -0400
            Re: Function factories "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-23 18:18 +0200
              Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:40 -0400
            Re: Function factories Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2012-08-23 09:29 -0700
              Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 18:44 +0200
              Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:46 -0400
            Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 18:02 +0100
              Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 19:24 +0200
                Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 18:07 -0700
                Re: Function factories Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-23 21:30 -0400
                Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 21:46 -0700
              Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 13:35 -0400
                Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 20:13 +0200
              Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 17:57 -0700
              Re: Function factories Andrew Cooper <amc96@cam.ac.uk> - 2012-08-25 02:35 +0100
            Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 21:21 +0200
              Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 15:36 -0400
                Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 22:09 +0200
                Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 16:20 -0400
                Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-23 22:33 +0200
                Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 17:07 -0400
                Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 14:51 -0700
                Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 00:09 +0200
                Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 19:01 -0400
                Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 16:31 -0700
                Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 18:31 +0200
                Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-24 02:37 -0700

csiph-web