Created
August 13, 2013 22:04
-
-
Save oliora/6226173 to your computer and use it in GitHub Desktop.
Sample of different template specialization for raw array and ptr (const char[N] and const char *).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
struct StrRange; | |
template<size_t N> | |
struct StrRange<const char[N]> | |
{ | |
typedef const char * ConstIterator; | |
StrRange(const char (&t)[N]) | |
: m_begin(t) | |
{} | |
ConstIterator begin() const | |
{ | |
return m_begin; | |
} | |
ConstIterator end() const | |
{ | |
return m_begin + N; | |
} | |
private: | |
const char *const m_begin; | |
}; | |
template<> | |
struct StrRange<const char *> | |
{ | |
typedef const char * ConstIterator; | |
StrRange(const char *t) | |
: m_begin(t) | |
, m_end(t + strlen(t)) | |
{} | |
ConstIterator begin() const | |
{ | |
return m_begin; | |
} | |
ConstIterator end() const | |
{ | |
return m_end; | |
} | |
private: | |
const char *const m_begin; | |
const char *const m_end; | |
}; | |
template<typename T1, typename T2> | |
void do_something(const T1& t1, const T2& t2) | |
{ | |
StrRange<const T1> r1(t1); | |
StrRange<const T2> r2(t2); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment