Simple question about templates
-
I have a templated class that is based on string or wstring, and most things are easy as the basic_string contains sufficient operators and methods to manipulate the text. However, one thing I cannot get round is including character constants inside the template, as below:
template class Tokenizer
{
typedef typename T::size_type _Mysizt;
T strText;
T strFilter;\_Mysizt offset; // offset of the current substring (token) \_Mysizt index; // index to the next separator character, or npos if not found \_Mysizt length; // length of the current substring, or npos if the last one public: Tokenizer(T strText, T strFilter) { // code removed for readability } T Next(bool bTrim) { T strToken; if (bTrim) { // trim leading spaces
// but ' ' will not be accepted for a wstring, and L' ' will not be for a string
offset = strText.find_first_not_of(' ', offset);
}
// code removed for readability
}
return strToken;
}I am sure it must be possible (OG, Po'H, SB), but my reading of the documentation and samples has not helped. So what (obvious) did I miss? [edit] While David Crow's suggestion works for a character constant, it will not work for a string like:
offset = strText.find\_first\_not\_of(" \\t", offset);
[/edit]
-
I have a templated class that is based on string or wstring, and most things are easy as the basic_string contains sufficient operators and methods to manipulate the text. However, one thing I cannot get round is including character constants inside the template, as below:
template class Tokenizer
{
typedef typename T::size_type _Mysizt;
T strText;
T strFilter;\_Mysizt offset; // offset of the current substring (token) \_Mysizt index; // index to the next separator character, or npos if not found \_Mysizt length; // length of the current substring, or npos if the last one public: Tokenizer(T strText, T strFilter) { // code removed for readability } T Next(bool bTrim) { T strToken; if (bTrim) { // trim leading spaces
// but ' ' will not be accepted for a wstring, and L' ' will not be for a string
offset = strText.find_first_not_of(' ', offset);
}
// code removed for readability
}
return strToken;
}I am sure it must be possible (OG, Po'H, SB), but my reading of the documentation and samples has not helped. So what (obvious) did I miss? [edit] While David Crow's suggestion works for a character constant, it will not work for a string like:
offset = strText.find\_first\_not\_of(" \\t", offset);
[/edit]
I'm not sure I entirely understand the problem so my suggestion may be as valuable as a $3 bill. Can you use:
offset = strText.find_first_not_of(0x20, offset);
Or could you add the "search for" character as part of the constructor?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I'm not sure I entirely understand the problem so my suggestion may be as valuable as a $3 bill. Can you use:
offset = strText.find_first_not_of(0x20, offset);
Or could you add the "search for" character as part of the constructor?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I have a templated class that is based on string or wstring, and most things are easy as the basic_string contains sufficient operators and methods to manipulate the text. However, one thing I cannot get round is including character constants inside the template, as below:
template class Tokenizer
{
typedef typename T::size_type _Mysizt;
T strText;
T strFilter;\_Mysizt offset; // offset of the current substring (token) \_Mysizt index; // index to the next separator character, or npos if not found \_Mysizt length; // length of the current substring, or npos if the last one public: Tokenizer(T strText, T strFilter) { // code removed for readability } T Next(bool bTrim) { T strToken; if (bTrim) { // trim leading spaces
// but ' ' will not be accepted for a wstring, and L' ' will not be for a string
offset = strText.find_first_not_of(' ', offset);
}
// code removed for readability
}
return strToken;
}I am sure it must be possible (OG, Po'H, SB), but my reading of the documentation and samples has not helped. So what (obvious) did I miss? [edit] While David Crow's suggestion works for a character constant, it will not work for a string like:
offset = strText.find\_first\_not\_of(" \\t", offset);
[/edit]
Presumably your goal is to use a set of characters rather than one character for your separator. And the classes in use do not provide that functionality. I believe tokenizers iterate through the string character by character, perhaps for efficiency and perhaps to avoid the problem you have. If you want to avoid iteration then perhaps strtok() might solve it.
-
Presumably your goal is to use a set of characters rather than one character for your separator. And the classes in use do not provide that functionality. I believe tokenizers iterate through the string character by character, perhaps for efficiency and perhaps to avoid the problem you have. If you want to avoid iteration then perhaps strtok() might solve it.
Thanks, that is the conclusion I came to, and I resolved the issue with a slightly different implementation. I was really just trying to learn a bit more about templates, and wondered if there was any provision for automatically converting constant strings between ASCII and Unicode. It's obviously something to be aware of, and avoid.