Define an alias for specific instances of the template method

Suppose I have a function that manages strings and handles all char types perfectly.

template<typename CharT> std::basic_string<CharT> foo_basic_string() { return std::basic_string<CharT, char_traits<CharT>, allocator<CharT> >(); } 

I want the functions foo_string and foo_wstring be a version of foo_basic_string and return std::string and std::wstring respectively.

One of the methods -

 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 being actually an instance of foo_basic_string<char> .

+6
source share
1 answer

You can write

 auto& foo_string = foo_basic_string<char>; auto& foo_wstring = foo_basic_string<wchar_t>; 

This declares foo_string as a function reference, referring to the specialization of your template.

+8
source

Source: https://habr.com/ru/post/984949/


All Articles