Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define alias for specific template method instances

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>.

like image 388
UmNyobe Avatar asked Apr 10 '15 12:04

UmNyobe


1 Answers

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.

like image 88
Columbo Avatar answered Sep 28 '22 07:09

Columbo