Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template specialization on void*

Reply
Thread Tools

template specialization on void*

 
 
petschy
Guest
Posts: n/a
 
      11-13-2006
hello all,

i'm trying to write a trait class (to generate an uint hash code for a
type):

template<typename T>
struct S
{
static unsigned int f(const T& t_);
};

template<typename T>
inline unsigned int S<T>::f(const T& t_)
{
// generic version using the bytes of T
}

the specializations for int, std::string work fine:

template<>
inline unsigned int S<unsigned int>::f(const unsigned int& s_);

template<>
inline unsigned int S<std::string>::f(const std::string& s_);

for void*, i get an error from gcc 4.1

template<>
inline unsigned int S<void*>::f(const void*& s_);

error: template-id 'f<>' for 'unsigned int S<void*>::f(const
void*&)' does not match any template declaration

i suspect the type of the f() arg is wrong, const void*& means const
reference to void* or reference to const void* ?

cheers, p

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-13-2006
petschy wrote:
> i'm trying to write a trait class (to generate an uint hash code for a
> type):
>
> template<typename T>
> struct S
> {
> static unsigned int f(const T& t_);
> };
>
> template<typename T>
> inline unsigned int S<T>::f(const T& t_)
> {
> // generic version using the bytes of T
> }
>
> the specializations for int, std::string work fine:
>
> template<>
> inline unsigned int S<unsigned int>::f(const unsigned int& s_);
>
> template<>
> inline unsigned int S<std::string>::f(const std::string& s_);
>
> for void*, i get an error from gcc 4.1
>
> template<>
> inline unsigned int S<void*>::f(const void*& s_);
>
> error: template-id 'f<>' for 'unsigned int S<void*>::f(const
> void*&)' does not match any template declaration
>
> i suspect the type of the f() arg is wrong, const void*& means const
> reference to void* or reference to const void* ?


It seems that what you're trying to do is to cv-qualify a 'void'.

Also, try dropping "template<>".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-14-2006
petschy wrote:
> i'm trying to write a trait class (to generate an uint hash code for a
> type):
>
> template<typename T>
> struct S
> {
> static unsigned int f(const T& t_);
> };
>
> ...
> for void*, i get an error from gcc 4.1
>
> template<>
> inline unsigned int S<void*>::f(const void*& s_);
>
> error: template-id 'f<>' for 'unsigned int S<void*>::f(const
> void*&)' does not match any template declaration
> ...


Should be

template<> inline unsigned int S<void*>::f(void* const& s_);

You have 'const T&' with 'T = void*'. This unwraps into 'void* const&', not into
'const void*&'.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
petschy
Guest
Posts: n/a
 
      11-14-2006
thanks, it compiles now.

if i write Hash<int*, int>, where int* is the key type and int is the
value type, then the default hash fn will be called. i must explicitly
set the trait with Hash<int*, int, Trait<void*> > to have the right fn
called.

is there a way to tell the compiler that for any pointer type i wish to
use the void* trait, without explicitly specifying it?

cheers, p

 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      11-14-2006

petschy wrote:
> thanks, it compiles now.
>
> if i write Hash<int*, int>, where int* is the key type and int is the
> value type, then the default hash fn will be called. i must explicitly
> set the trait with Hash<int*, int, Trait<void*> > to have the right fn
> called.
>
> is there a way to tell the compiler that for any pointer type i wish to
> use the void* trait, without explicitly specifying it?
>
> cheers, p


you need to specialize the class template

template<typename T>
struct S<T*>
{
static unsigned int f(T * const & t_)
{
your code for void *
}
};

 
Reply With Quote
 
petschy
Guest
Posts: n/a
 
      11-14-2006

dasjotre wrote:
> petschy wrote:
> > thanks, it compiles now.
> >
> > if i write Hash<int*, int>, where int* is the key type and int is the
> > value type, then the default hash fn will be called. i must explicitly
> > set the trait with Hash<int*, int, Trait<void*> > to have the right fn
> > called.
> >
> > is there a way to tell the compiler that for any pointer type i wish to
> > use the void* trait, without explicitly specifying it?
> >
> > cheers, p

>
> you need to specialize the class template
>
> template<typename T>
> struct S<T*>
> {
> static unsigned int f(T * const & t_)
> {
> your code for void *
> }
> };


this doesn't seem to work. for int* keys, the compiler searches for
S<int*>, and won't find it, since i only provided implementation for
the specialization for void*. it should pick the void* specialization,
but now i'm not sure whether this is possible automatically, w/o
specifying the right trait type.

cheers, p

 
Reply With Quote
 
petschy
Guest
Posts: n/a
 
      11-14-2006
> > you need to specialize the class template
> >
> > template<typename T>
> > struct S<T*>
> > {
> > static unsigned int f(T * const & t_)
> > {
> > your code for void *
> > }
> > };


it works now omitted the specialization for void*, and provided
implementation for f(T* const&). this way all the pointer types will
have a separate function, which means code duplication. in this very
case it's not a problem, since the body of the fn is rather simple and
will be inlined anyway, but for other cases i still wonder how could
one instruct the compiler to use the void* implementation.

my solution:

template<>
inline S<void*>::f(void* const& t_)
{
return calculated hash by casting t_ to int and shifing
}

template<T>
inline S<T*>::f(T* const& t_)
{
return S<void*>::f(t_);
}

this approach would still work when the functions are more complex, but
the same for all pointer types. in that case, the general f()
implementation should be inlined, while the specialized void* not, to
avoid code bloat.

thanks for the replies.

cheers, p

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
template specialization overriding non-specialization? Joseph Turian C++ 2 04-16-2006 02:46 PM
Template specialization for templated and primitive type template parameters case2005 C++ 3 02-13-2005 06:53 PM
template specialization of a template class pit3k C++ 8 02-09-2005 02:39 AM
Specialization of a template within template David B. Held C++ 2 10-26-2003 11:59 AM



Advertisments