我有一个模板类simplestring,可以简单地处理TChar *及其长度。 TChar可以是char和wchar_t。 这是一个简单的左修剪方法,
simplestring<T> ltrim(const T* _s = nullptr) const
{
const T* s = _s;
if (s == nullptr)
{
#if ( sizeof(T) == 1)
s = " \t\r\n";
#else
s = L" \t\r\n";
#endif
}
constexpr int len = tstrlen(s);
find_first_not_of(s, len);
}
我希望当T为char时为s分配一个char *,否则为wchar_t *分配一个。它不会编译。 PS:我的项目支持C ++ 17。
With C++14, Variable templates have been introduced.
因此,使用模板专门化,可以为不同类型的变量提供相同的名称但值不同。
为了说明可能的解决方案,我制作了以下MCVE:
输出:
Live Demo on coliru