• 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: "Edward K. Chew" <email@hidden>
  • Date: Tue, 29 Aug 2006 00:09:24 -0400

First of all, thanks for the various replies!

So, after another day of pounding on it, I was able to condense the specialization problem down to a simple example:

//__________________________________________________

#include <cstring>
#include <iostream>

namespace AccessFunctions
{
	template<typename T> int dataLength(T value)
	{
		return sizeof value;
	}

	template<typename T> int dataLength(T* value)
	{
		return sizeof(T);
	}

	template<> int dataLength(const char* str)
	{
		return std::strlen(str);
	}
}

template<typename T>
class TAccessClass
{
public:
	static int dataLength(T value)
	{
		return AccessFunctions::dataLength<T>(value);
	}
};

int main (int argc, char * const argv[]) {
	using namespace std;
	const char str[] = "Hello, world!";

	cout << "Case 1: ";
	cout << AccessFunctions::dataLength(str);
	cout << endl;

	cout << "Case 2: ";
	cout << TAccessClass<const char*>::dataLength(str);
	cout << endl;

	return 0;
}

//__________________________________________________

So when I pass my string to the dataLength function, the compiler could conceivable choose one of three implementations. The first function should return 4: the size of a pointer (assuming you are compiling in 32-bit mode). The second should return 1 (the size of the char being pointed to). The third should return 13 (the length of the string). What gcc gives me is:

Case 1: 13
Case 2: 4

Having an intermediary class, then, was the source of my grief. On further examination, I determined that if I remove the "<T>" from the line "return AccessFunctions::dataLength<T>(value);", case 2 would also return 13. Is there something illegal/deprecated about the way I had it? If so, it would have been nice to get a compiler warning. I vaguely recall having to put that in there to assuage some ancient compiler (FWIW, CW 10 doesn't seem to care one way or the other). I am lazy, and wouldn't have added it without reason. :-)

Anyway, so that's one down, at least...

-Ted

On Aug 28, 2006, at 8:11 PM, Steve Checkoway wrote:

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: Glen Low <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>)
 >Re: Template trouble (From: Steve Checkoway <email@hidden>)

  • Prev by Date: Cmath and math.h Changes in Xcode 2.4
  • Next by Date: Re: Cmath and math.h Changes in Xcode 2.4
  • Previous by thread: Re: Template trouble
  • Next by thread: Re: Template trouble
  • Index(es):
    • Date
    • Thread