• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Template trouble
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Template trouble


  • Subject: Re: Template trouble
  • From: Steve Checkoway <email@hidden>
  • Date: Mon, 28 Aug 2006 17:11:53 -0700


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: This email sent to email@hidden
  • Follow-Ups:
    • Re: Template trouble
      • From: "Edward K. Chew" <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>)

  • Prev by Date: Re: Debugger thread dump confusion
  • Next by Date: Re: Template trouble
  • Previous by thread: Re: Template trouble
  • Next by thread: Re: Template trouble
  • Index(es):
    • Date
    • Thread