|
Boost : |
From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2007-07-12 17:28:31
----- Mensaje original -----
De: Marius Lazer <Marius.Lazer_at_[hidden]>
Fecha: Jueves, Julio 12, 2007 7:22 pm
Asunto: Re: [boost] multi-index warnings
Para: boost_at_[hidden]
> Joaquín,
>
> Here it is (as succinct as I could). All is running on Solaris 10.
>
> Thanks,
> Marius
[...]
Hello Marius,
OK, I've been able to reproduce the problem and after analyzing it
I think that, unless I'm grossly mistaken, the problem lies inside
Boost.Hash --which is not to say this lib has any error, it's just
an effect of this overzealous -Wsign-promo switch. Let me
explain my findings:
I've been able to reproduce the warnings using exclusively
Boost.Hash:
***code1***
// warns with Boost 1.34, clean compile with Boost 1.31
#include <boost/functional/hash/hash.hpp>
int main(){}
***code1***
This warns with Boost 1.34 and does not warn wih Boost 1.33.1,
under GCC 3.4.4 for Cygwin, always using the -Wsign-promo switch.
This is in contradiction with some previous reports by you, my
guess is that you forgot to include the switch (could you confirm
this?) According to GCC docs, -Wsign-promo "warn[s] when
overload resolution chooses a promotion from unsigned or enumeral
type to a signed type over a conversion to an unsigned type of
the same size.[...]" The relevant portion of Boost 1.34
Boost.Hash can be isolated and simplified to more easily show
the issue:
***code2***
#include <cstddef>
std::size_t hash_value(int){return 0;}
std::size_t hash_value(long int){return 0;}
template<typename> struct hash;
template<> struct hash<bool>
{
std::size_t operator()(bool x)const{return hash_value(x);}
};
int main(){}
***code2***
This emits the same kind of warnings you've seen: -Wsign-promo
informs about the int overload having been chosen in preference
to the long int overload (you can try yourself).
As for why Boost.Hash 1.33.1 does not produce warnings, the
reason is that in Boost 1.33.1 hash<T> is defined (roughly)
as:
template<typename> struct hash
{
std::size_t operator()(T x)const{return hash_value(t);}
};
whereas in Boost 1.34 the class template hash is left
undefined and a number of full specializations (like
bool and other integral types) are explicitly defined,
in the spirit of the code2 snippet. Matter of fact, the
following will emit the warnings *both* with Boost 1.33.1
and 1.34:
***code3***
// warns with Boost 1.33.1 and 1.34
#include <boost/functional/hash/hash.hpp>
int main()
{
boost::hash<bool> h;
h(true);
}
***code3***
Again, I'd be grateful if you can confirm all of this in
your own environment.
What to do about this? Of course if you can skip the
-Wsign-promo switch no warnings will arise. If this is not an
option, the author of Boost.Hash should have to be convinced
to fix this, possibly by providing more overloads of
hash_value (for bool and the rest of offending types) so
as to avoid integral promotions altogether.
Hope this helps, please come back if your experiments
do not agree with this analysis.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk