Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Table of "safe" methods to suppress "unused parameter" warnings? |
| Date | 2014-03-31 13:55 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <lhbl31$sld$1@dont-email.me> (permalink) |
| References | (1 earlier) <bpi725FabtcU4@mid.individual.net> <kfnioqwil03.fsf@x-alumni2.alumni.caltech.edu> <bprd8bFfrepU2@mid.individual.net> <lhbcla$14u$1@dont-email.me> <87d2h2lm5p.fsf@dpt-info.u-strasbg.fr> |
On 31/03/14 12:04, Alain Ketterlin wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 30/03/14 22:31, Ian Collins wrote:
>>> Tim Rentsch wrote:
>
>>>>> mathog wrote:
>>>>>> Sometimes the same parameter list must be passed to a lot of different
>>>>>> functions, and some of those will not use all of the parameters,
>>>>>> resulting in some compilers emitting "unused parameter" warnings.
>
>>> I disagree. Allowing unnamed parameters would address the exact problem
>>> stated in the question.
>
>> I disagree (at least a bit) here - I don't see any reason to have
>> unnamed parameters in C. If the parameter is completely useless, then
>> it should be removed entirely - if not, then the name is part of the
>> documentation for the function's interface.
>
> I think the OP mentionned callbacks (see above, I hope I have the
> quoting right), where the interface is imposed. Typically, GUI toolkits
> make massive use of this, but pthread_create is another example---there
> are cases where a thread does not take a parameter, and relies on shared
> data only.
I missed that bit - sometimes threads in c.l.c. get so long that I have
to skip many details.
But you are right - that /is/ a good reason to have unnamed parameters
in C. If you have to fit a function into a particular general function
type, then you will sometimes be forced to have parameters that are
never used, and typically have generic names like "param1" in the
function type declaration - leaving them unnamed in the function
definition would be clear documentation that they are never used in that
particular function.
So I'll make a minor U-turn and say that unnamed parameters in C /would/
sometimes be a useful feature.
>
> I've had this situation often, I would like to see unnamed parameters in
> C (a simple comment solves the "documentation issue").
>
>> And the solution to the original problem is nothing more dramatic than
>> "(void) x;", which is neither hard to write nor hard to understand.
>
> Agree. It is still somewhat strange to insert a statement with no
> effect.
True, but without any standard way of handling this then "(void) x;" is
the simplest and clearest way to have a statement with no effect.
>
>> In C++, I can think of a situation where unnamed parameters make sense -
>> you might want a function that requires you to have an object of a
>> particular class but does not care about the contents. [...]
>
> Not only. Virtual member functions are a (fairly frequent) variant of
> the "callback" approach.
>
>> This can be used to give compile-time checking of constraints. For
>> example, if your system has a global interrupt lock, then you might
>> use a class "InterruptDisabler" whose constructor preserves the old
>> interrupt state and disables interrupts, and whose destructor restores
>> the state. A function that expects interrupts to be disabled could
>> take an unnamed parameter of type "const InterruptDisabler&".
>
> Not sure about your last sentence... And the whole idea seems rather,
> well, unusual.
>
I think the idea is perhaps unusual - and it is certainly heretic in
c.l.c. If you haven't done embedded development, then the idea of
"disabling interrupts" is probably a bit odd. But let's take another
example. Suppose you have some sort of lock, with two functions
"getBigLock()" and "releaseBigLock()":
extern void getBigLock(void);
extern void releaseBigLock(void);
// Don't call this unless you've got the big lock!
extern void doSomethingDangerous(void);
void foo(void) {
getBigLock();
doSomethingDangerous();
releaseBigLock();
}
void bar(void) {
doSomethingDangerous();
}
In this case, the compiler cannot help spot that bar() is going to cause
trouble - you are reliant on comments and programmer care to get the
locking right.
In C++, you could do this:
class BigLock {
public:
BigLock() { getBigLock(); }
~BigLock() { releaseBigLock(); }
}
extern void doSomethingDangerous(const BigLock&);
void foo(void) {
BigLock bl;
doSomethingDangerous(bl);
}
void bar(void) {
doSomethingDangerous();
}
bar() is now clearly a compile-time error. doSomethingDangerous()
doesn't actually do anything with the parameter - it just uses
type-checking to ensure that you have a BigLock.
With enough care, templates, and hierarchies, you can do quite a bit of
compile-time checking using types like this.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Table of "safe" methods to suppress "unused parameter" warnings? mathog <dmathog@gmail.com> - 2014-03-26 09:54 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Barry Schwarz <schwarzb@dqel.com> - 2014-03-26 10:55 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Keith Thompson <kst-u@mib.org> - 2014-03-26 11:13 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Javier Lopez <j.lopezg@aol.com> - 2014-03-26 19:45 +0100
Re: Table of "safe" methods to suppress "unused parameter" warnings? Kaz Kylheku <kaz@kylheku.com> - 2014-03-26 19:55 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? mathog <dmathog@gmail.com> - 2014-03-26 17:01 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Javier Lopez <j.lopezg@aol.com> - 2014-03-27 01:46 +0100
Re: Table of "safe" methods to suppress "unused parameter" warnings? Thomas Jahns <jahns@idontlikespam.dkrz.de> - 2014-03-27 09:34 +0100
Re: Table of "safe" methods to suppress "unused parameter" warnings? gazelle@shell.xmission.com (Kenny McCormack) - 2014-03-27 10:52 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-03-27 13:10 +0100
Re: Table of "safe" methods to suppress "unused parameter" warnings? Ian Collins <ian-news@hotmail.com> - 2014-03-27 21:50 +1300
Re: Table of "safe" methods to suppress "unused parameter" warnings? Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 17:32 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Ian Collins <ian-news@hotmail.com> - 2014-03-31 09:31 +1300
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-03-31 11:31 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-03-31 12:04 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-03-31 13:55 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-04-01 02:04 +0300
Re: Table of "safe" methods to suppress "unused parameter" warnings? Kaz Kylheku <kaz@kylheku.com> - 2014-03-31 23:37 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-04-01 10:16 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-05-22 13:10 +0300
Re: Table of "safe" methods to suppress "unused parameter" warnings? Kaz Kylheku <kaz@kylheku.com> - 2014-05-22 12:05 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-04-01 09:59 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? Ian Collins <ian-news@hotmail.com> - 2014-03-31 23:17 +1300
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-03-31 14:05 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? Kaz Kylheku <kaz@kylheku.com> - 2014-03-31 15:24 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-15 04:26 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Öö Tiib <ootiib@hot.ee> - 2014-04-23 07:31 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Keith Thompson <kst-u@mib.org> - 2014-04-23 08:45 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 17:21 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-03-31 13:26 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? James Kuyper <jameskuyper@verizon.net> - 2014-03-31 07:37 -0400
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-03-31 14:12 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-18 07:00 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? gazelle@shell.xmission.com (Kenny McCormack) - 2014-04-18 15:02 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? Keith Thompson <kst-u@mib.org> - 2014-04-18 08:58 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-27 08:11 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? gazelle@shell.xmission.com (Kenny McCormack) - 2014-04-27 15:26 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? Tim Rentsch <txr@alumni.caltech.edu> - 2014-06-10 08:30 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-06-11 00:57 +0300
Re: Table of "safe" methods to suppress "unused parameter" warnings? Ike Naar <ike@iceland.freeshell.org> - 2014-06-10 22:12 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-06-11 20:25 +0300
Re: Table of "safe" methods to suppress "unused parameter" warnings? Martin Shobe <martin.shobe@yahoo.com> - 2014-06-10 18:59 -0500
Re: Table of "safe" methods to suppress "unused parameter" warnings? Tim Rentsch <txr@alumni.caltech.edu> - 2014-06-11 21:22 -0700
Re: Table of "safe" methods to suppress "unused parameter" warnings? Kaz Kylheku <kaz@kylheku.com> - 2014-04-28 01:40 +0000
Re: Table of "safe" methods to suppress "unused parameter" warnings? David Brown <david.brown@hesbynett.no> - 2014-04-19 17:52 +0200
Re: Table of "safe" methods to suppress "unused parameter" warnings? Siri Crews <chine.bleu@yahoo.com> - 2014-06-11 02:04 -0700
csiph-web