I'd like to write a function template that operates on a container of strings, for example a std::vector
.
I'd like to support both CString
and std::wstring
with the same template function.
The problem is that CString
and wstring have different interfaces, for example to get the "length" of a CString
, you call the GetLength()
method, instead for wstring you call size()
or length()
.
If we had a "static if" feature in C++, I could write something like:
template <typename ContainerOfStrings>
void DoSomething(const ContainerOfStrings& strings)
{
for (const auto & s : strings)
{
static_if(strings::value_type is CString)
{
// Use the CString interface
}
static_else_if(strings::value_type is wstring)
{
// Use the wstring interface
}
}
}
Is there some template programming technique to achieve this goal with currently available C++11/14 tools?
PS
I know it's possible to write a couple of DoSomething()
overloads with vector<CString>
and vector<wstring>
, but that's not the point of the question.
Moreover, I'd like this function template to work for any container on which you can iterate using a range-for loop.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…