-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compiler warnings from -Wcast-function-type in signal.h, slot.h #8
Comments
Here's a discussion on a possible way to get around this, in a way that seems to satisfy gcc and the standard: https://bugs.python.org/issue33012#msg317823 and from the gcc docs: The function type |
This is a duplicate of issue #1. It has been fixed in the master branch and in The fix is to call this template function instead of reinterpret_cast<>():
There is not yet a tarball that includes this fix. |
Yiiikes. How is that valid C++? Unions cannot be used for typecasting/reinterpretation in C++, only C. Doing that in C++ is undefined behaviour from everything I've read. (I don't think compilers are capricious enough to blow up in this case; I have a vague recollection that this might even be a documented extension in g++, but that doesn't mean it's good code to write. :P) |
I must confess that I didn't know that it's illegal. A union has been used for |
UB doesn't require a diagnostic; that's the main problem with thinking that the lack of an error means things are OK. :) Pure speculation, but GCC might also eschew the warning because this is legal in C and/or (as I said, IIRC) it allows it as a documented extension in C++ mode too. Won't the alternative method that I linked to solve this, while keeping the Standard happy too? (I didn't read too far into it yet though.) It may or may not be related to what you saw in GCC 4, but that's very old, so it might not reflect current compilers anyway. |
gcc8 -Wextra prints a warning when a single reinterpret_cast is used for conversion between different types of function pointers. The previous fix with a union in sigc::internal::bitwise_equivalent_cast<>() is not standard C++. Rename the function to function_pointer_cast<>(), and use two reinterpret_casts as recommended in gcc's documentation. Fixes #8
gcc8 -Wextra prints a warning when a single reinterpret_cast is used for conversion between different types of function pointers. The previous fix with a union in sigc::internal::bitwise_equivalent_cast<>() is not standard C++. Rename the function to function_pointer_cast<>(), and use two reinterpret_casts as recommended in gcc's documentation. Fixes #8
In the master branch and the libsigc++-2-10 branch I have changed to
Do you care about |
Thanks! I care inasmuch as I would ideally like all libraries I use to be 100% standard-compliant, but I'm not holding out much hope for that where we start getting closer to GLib et al. :P I appreciate you considering these boring legalese points in any case. As for how to do it, I have 2 thoughts right now, but these may not be comprehensive or optimal: (1) An alternative to casts/reinterpretation, which as far as I know is allowed for all trivially copyable types, is to memcpy the bytes into a new instance of the destination type. That often gets optimised to the same thing as any cast while keeping the Standard happier. The guarantee here is pretty much what you get with allowed casts: you can 'cast' the Thing to Something Else and eventually back to a Thing that is still usable. I get the impression this is pretty much what to use when casts don't look legal. I haven't read about this with specific attention to data/function pointer conversion, but those should both be trivially copyable types, surely? But we would want to double-check whether existing discussions cover this (and, if it's viable, to assert that the intermediate type has sufficient (2) Secondarily, it seems that since C++11, converting between data and function pointers is "conditionally supported", and implementation-defined when it is: 1, 2 This may be better in some philosophical sense than the |
(1) (2) I've made a few tests with gcc 8. It accepts I'll probably keep using a Bjarne Stroustrup (The C++ Programming Language, 4th edition, section 8.3) |
But you'd be passing "illegal" isn't really a useful term when dissecting Standardese, IME. "undefined behaviour" is more relevant and inferable: the Standard doesn't state that union 'casting' should work, in fact quite the opposite since only one union member is theoretically alive at any one time, and merely reading does not change that (theoretical) fact. "implementation-defined behaviour" is better than that, at least. Anyway, the qualifiers Bjarne used, even if they exclude "undefined" or "illegal", should be hint enough! Just because gcc 8 never complains about the union trick does not make it good style; it just means that it's UB and therefore does not require a compiler diagnostic, and gcc does not issue one. So saying that the alternative of However, I won't harp on about this for much longer if my points aren't landing. To some extent, the ship of fully Standard-compliant code sailed when we started using GLib! Other infractions, especially widely relied-upon ones, aren't practically likely to make anything fail. It'd just be nice to avoid adding. |
gcc8 -Wextra prints a warning when a single reinterpret_cast is used for conversion between different types of function pointers. The previous fix with a union in Glib::bitwise_equivalent_cast<>() is not standard C++. Rename the function to Glib::function_pointer_cast<>(), and use two reinterpret_casts as recommended in gcc's documentation. * glib/src/optiongroup.ccg: Use a reinterpret_cast to convert from a function pointer to void*. That's possible now. It's "conditionally supported", starting with C++11. See libsigcplusplus/libsigcplusplus#8
gcc8 -Wextra prints a warning when a single reinterpret_cast is used for conversion between different types of function pointers. The previous fix with a union in Glib::bitwise_equivalent_cast<>() is not standard C++. Rename the function to Glib::function_pointer_cast<>(), and use two reinterpret_casts as recommended in gcc's documentation. * glib/src/optiongroup.ccg: Use a reinterpret_cast to convert from a function pointer to void*. That's possible now. It's "conditionally supported", starting with C++11. See libsigcplusplus/libsigcplusplus#8
Back in 2011 I added the code that required a conversion from a function pointer I find it a bit strange that |
Yeah, that does sound odd and perhaps worth a gcc bug if one does not already exist (though I know none of us like filing those :) ) |
After an update on Debian unstable, I'm getting a bunch of warnings like the below.
This might be due to a compiler update more than libsicg++ since the packages in my apt archive seem quite old:
the warnings (not necessarily all of them, but you get the idea):
I don't know if these are solvable, but hopefully; I don't like having to just hide warnings when compiling my projects. However, I grimly suspect I might have to do so, since ultimately GLib and therefore perhaps glibmm as a consequence rely on doing naughty things with casts, which probably can't be avoided once we go deep enough. :P
The text was updated successfully, but these errors were encountered: