Assume I have a function which manipulate strings, and which perfectly handle all char types.
template<typename CharT>
std::basic_string<CharT> foo_basic_string()
{
return std::basic_string<CharT, char_traits<CharT>, allocator<CharT> >();
}
I want functions foo_string
and foo_wstring
to be a version of foo_basic_string
and return a std::string
and std::wstring
, respectively.
One way is
std::string foo_string()
{
return foo_basic_string<char>();
}
std::wstring foo_wstring()
{
return foo_basic_string<wchar_t>();
}
I was wondering if there is a way to declare foo_string
as actually being the instance foo_basic_string<char>
.
You can write
auto& foo_string = foo_basic_string<char>;
auto& foo_wstring = foo_basic_string<wchar_t>;
This declares foo_string
as a reference to function, referring to the specialization of your template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With