I wrote a template class in order to create a "string matcher", which means accepting a wide character as an argument, and seeing whether it is what I want. Below is the code:
template <wchar_t... text> class Token;
template <>
class Token<>
{
protected:
bool failed;
};
template <wchar_t head,wchar_t... tail>
class Token<head,tail...> : public Token<tail...>
{
public:
void match(wchar_t c) { if(c != head) Token<>::failed = true; }
};
The problem is, I should use this template class in such a strange way:
Token<L'H',L'e',L'l',L'l',L'o'> token1;
This looks really awful, right?
So, I want a better way to use this template. How an I manage to use it like this:
Token<L"Hello"> token1;
or(maybe using constexpr faunctions?)
auto token1 = makeToken(L"Hello");
or(using user-defined literals)
auto token1 = L"Hello"_Token;
Can this be done in C++11? If not, what is something that can be the alternative?
The reason why I use a template(not a function) is to help the program run faster, so I'll consider rewriting this into a function if no other choice exists. That also means, if you have another way to do this(other than the template, but not too slow, please let me know.
Many thanks.
Sincerely,Ruifeng
Aucun commentaire:
Enregistrer un commentaire