Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Template trouble




On Aug 28, 2006, at 1:00 PM, Edward K. Chew wrote:

Oh, I forgot to mention, I have run into trouble with template specialization. For example, I have a function along the lines:

template <typename T> UInt32 myHashFunction(T value, int hashBitLength);

I specialized this for C strings:

template <> UInt32 myHashFunction(const char *str, int hashBitLength);

In most cases, I use a string and get the specialized version, but in a few situations, I get the general implementation instead. I experimented with partial specialization (i.e. ...myHashFunction (const T *pointer... ), but it still goes with the general case. The debugger says that the class from which I call myHashFunction is of type myClass<char const *>, but I believe "const char *" and "char const *" are equivalent in C++. Anyway, I tried every permutation I could think of to no avail.

I have a guess why this might be happening.

[PBG4:~/temp] steve$ cat template.cc
#include <cstdio>
template <typename T> void f( T t ) { puts( "general" ); }
template<> void f<const char *>( const char *s ) { puts( s ); }

int main()
{
        char foo[] = "non const";
        const char *bar = "const";
        f( foo );
        f( bar );
        return 0;
}
[PBG4:~/temp] steve$ g++ -Wall -std=c++98 template.cc
[PBG4:~/temp] steve$ ./a.out
general
const

As you can see, the general version will be called for char*. Personally, I can never remember the matching rules so I always have to test this stuff out.

--
Steve Checkoway


_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/xcode-users/email@hidden

This email sent to email@hidden
References: 
 >FSRef_fopen alternative (From: "Edward K. Chew" <email@hidden>)
 >Template trouble (From: "Edward K. Chew" <email@hidden>)
 >Re: Template trouble (From: "Edward K. Chew" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.