Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overloaded function template with different return types

33 views
Skip to first unread message

hbdev...@gmail.com

unread,
Feb 1, 2014, 8:57:50 PM2/1/14
to
Hello all,

In functional\hash\hash.hpp in Boost ver. 1.54.0, there are many overloads of the template function hash_value where only the return type changes (see a snapshot of code at the end of this post).

1-
How does the compiler determine which version of the hash_value it should use in a call?
For example, for struct hash<unsigned int>::operator() it uses the version that returns boost::hash_detail::basic_numbers<T>::type.
Why ? What is the rule it applies ?

2-
How can I force the compiler to use a certain version of hash_value ?

Thank you very much in advance

----- code from boost\functional\hash\hash.hpp ------

namespace boost
{
namespace hash_detail
{
struct enable_hash_value { typedef std::size_t type; };

template <typename T> struct basic_numbers {};
template <typename T> struct long_numbers;
template <typename T> struct ulong_numbers;
...
template <> struct basic_numbers<unsigned int> :
boost::hash_detail::enable_hash_value {};
...
template <typename T> struct long_numbers2 {};
template <typename T> struct ulong_numbers2 {};
template <typename T> struct long_numbers : long_numbers2<T> {};
template <typename T> struct ulong_numbers : ulong_numbers2<T> {};
...
}

//version 1 of hash_value
template <typename T>
typename boost::hash_detail::basic_numbers<T>::type hash_value(T v)
{
return static_cast<std::size_t>(v);
}

//version 2 of hash_value
template <typename T>
typename boost::hash_detail::long_numbers<T>::type hash_value(T v)
{
return hash_detail::hash_value_signed(v);
}

//version 3 of hash_value
template <typename T>
typename boost::hash_detail::ulong_numbers<T>::type hash_value(T v)
{
return hash_detail::hash_value_unsigned(v);
}
...

template <> struct hash<unsigned int> :
public std::unary_function<unsigned int, std::size_t>
{
std::size_t operator()(unsigned int v) const
{
return boost::hash_value(v); //uses version 1 of hash_value
}
};

Ken-Yi Lee

unread,
Feb 2, 2014, 8:22:31 AM2/2/14
to
On Sunday, February 2, 2014 9:57:50 AM UTC+8, hbdev...@gmail.com wrote:
> Hello all,
> In functional\hash\hash.hpp in Boost ver. 1.54.0, there are many overloads of the template function hash_value where only the return type changes (see a snapshot of code at the end of this post).
> 1-
> How does the compiler determine which version of the hash_value it should use in a call?
>
> For example, for struct hash<unsigned int>::operator() it uses the version that returns boost::hash_detail::basic_numbers<T>::type.
>
> Why ? What is the rule it applies ?

You can take a look at the namespace: boost::hash_detail. A struct, enable_hash_value, is used to determine the bindings. For example, bool, char and unsigned char types are listed in basic_numbers.

> 2-
> How can I force the compiler to use a certain version of hash_value ?

You can use casting.

Feis
0 new messages