Different functions based on template arg

Started by
4 comments, last by Sfpiano 16 years, 8 months ago

typedef unsigned short _Elem;
template <class _Elem>
class Data {
public:
void fn() {
...
_Elem* tmp = new _Elem[size];
			if( sizeof(*content) == 1 ) { //char
				strncpy(tmp, content, size);
			}
			else { //wchar_t
				wcsncpy(tmp, content, size);
			}
}

private:
_Elem* content;
}
I have a class that I want to take either normal or unicode strings. However I'm not sure how to call different functions based on if an instance of the class is unicode or not. The above code fails because the compiler translates 'tmp' into whichever character type I give it so one of the two 'cpy' functions is given a bad type, even though it won't be called.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Advertisement
In this case, consider just using std::copy() instead.
strcpy has no place in C++. Doubly so when dealing with strings.

If you find yourself in need to copying sequences of iterable containers, use std::copy.

Functions can be specialized using templates:
template <  class CharType >void foo( std::basic_string< CharType > &s ){  //}


To extend that, you can use template specialization, to handle other types differently:
template < class CharType >class operation{  void copy( std::basic_string< CharType > &s );};template <>class operation<char>{  void copy( std::string &s );}template <>class operation<wchar_t>{  void copy( std::wstring &s );}...template <  class CharType >void foo( std::basic_string< CharType > &s ){  operation< CharType > op;  op.copy( s );}


There's also a few gotchas using partial specialization, but the link eludes me right now.
Thanks, I didn't know you could use std::copy on a char array. The only reason I was using strcpy was because I need to use an actual char* instead of std::string because I'm working with binary data since std::string changed the data when I tried copying the data into it.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Quote:Original post by Sfpiano
The only reason I was using strcpy was because I need to use an actual char* instead of std::string because I'm working with binary data since std::string changed the data when I tried copying the data into it.
Can you elaborate on this? What do you mean by std::string 'changing the data'?

In case you're talking about dealing with different character types, std::string and std::wstring are just typedefs for std::basic_string<...>, so you should be able to use SC++L strings while still parameterizing by element type (or so I assume - I've never actually had occasion to do this myself). [Edit: See Antheus' post above for an example of this.]

[Edited by - jyk on August 11, 2007 6:41:50 PM]
Hmm, the way I originally had it, I was doing:

char* buff;
std::string str;
...

str += std::string(buff);

which was turning all of the non-standard characters into some other character.

But if I do:
str.append(buff, len);

then it works.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."

This topic is closed to new replies.

Advertisement