All
I am writing a trimStart fucntion with c++ template like the following:
template<typename T>
static T trimStart(T source, std::conditional<isWide<T>(), const wchar_t*, const char*>::type trimChars = " \t\n\r\v\f"))
{
....
}
now I like to provide a default value " \t\n\r\v\f" or L" \t\n\r\v\f" according the type of trimChars, could you please help me look at how to implement for that?
With the great help from 463035818_is_not_a_number & Philipp, I look at the latest template docs again, here is the updated skeleton of code (C++20):
//limit to T as string only
template<typename T>
concept isStr = (
std::is_same_v<T, std::string> ||
std::is_same_v<T, std::wstring>
);
// different default value according type of T
template<typename T>
constexpr auto defaultValue() {
if constexpr (std::is_same_v<T, std::wstring>)
return L" \t\n\r\v\f";
else
return " \t\n\r\v\f";
}
//claim a function parameter with a default value
template<isStr T>
static T trimStart(T source, decltype(defaultValue<T>()) trimChars = defaultValue<T>())
{
//...internal variable example
typename std::conditional<some_conditional<T>(), std::wistringstream,std::istringstream>::type
ss(...);
}