Path: csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: Proper cast of function pointers
Date: Wed, 24 Apr 2024 14:40:33 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <86jzkmwib2.fsf@linuxsc.com>
References:
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Wed, 24 Apr 2024 23:40:34 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="d0c198b2e081fbbcaac8aa6bee51e9fa"; logging-data="2688732"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+IShVOKU59M/n4uJsKPz/Gvj+BbStF8BQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:OhgR3RIIKhXK0jvslA9RKWMzah0= sha1:tuyByLUA/pRT6BXoBo6usbaWu6w=
Xref: csiph.com comp.lang.c++:118878
Paavo Helde writes:
> 23.04.2024 14:44 Bonita Montero kirjutas:
>
>> Am 23.04.2024 um 13:31 schrieb Paavo Helde:
>>
>>> There is an old third-party library where some function pointers
>>> are casted to another type, then back to the original type before
>>> use.
>>> C++ standard says this is kosher, and there have never been any
>>> problems with actual behavior. Alas, different versions and compile
>>> modes of g++ still produce warnings. ...
>>
>> That's because of the danger that someone calls the function-pointer
>> to which you cast. Maybe you can cast through a void-pointer to sup-
>> press this warning. But for me casting to a function pointer of a
>> differnt type doesn't make sense at at..
>> I think you confront yourself to uncertainties which actually never
>> happen.
>
> The function pointers are cast to a single type so that they can be
> stored in a common lookup array. I could use a union there, but this
> would mean additional work with no real benefit, as the hypothetical
> "someone" could just as easily mess up the unions than the casting.
That depends on how the code is written. The code can be written
so that making a mistake with the unions is both difficult and
unlikely. To be continued downthread...