Sign in to follow this  

Different functions based on template arg

This topic is 3779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this