Home Use proper string literal in templated function in C++
Reply: 2

Use proper string literal in templated function in C++

razzorflame
1#
razzorflame Published in 2018-02-13 16:54:09Z

I have a function, that is templated to match every std::basic_string instantiation:

template <typename _valueType>
void addFoo(std::basic_string<_valueType>& string_)
{
    string_ += "foo";
}

I want "foo" to be _valueType-dependent literal. I would like not to use specialization, because in the actual project I have whole class templated and it would be a lot of work.
I am currently using if constexpr in function body, but problems start, when I have function taking default parameter like this:

template <typename _valueType>
void foo(_valueType const delimiter_ = '.');
Yakk
2#
Yakk Reply to 2018-02-13 22:14:12Z

I'll assume you only support char and wchar_t. If you want to support a longer enumerated list of types, you can also do that. This doesn't work for a non-locally enumerated list of types.

template<class Index, class...Args>
decltype(auto) dispatch( Index, Args&&... args ) {
  return std::get<Index{}>( std::forward_as_tuple( std::forward<Args>(args)... ) );
}

This is a neat little helper that lets you compile-time dispatch between any number of arguments.

Now in your class define this:

template<class Char, class WChar>
static decltype(auto) pick(Char&& c, WChar&& w) {
  using is_wchar_t = std::is_same<_valueType, wchar_t>;
  return dispatch( is_wchar_t{}, std::forward<Char>(c), std::forward<WChar>(w) );
}

you can now do this:

template <typename _valueType>
void addFoo(std::basic_string<_valueType>& string_)
{
  string_ += pick( "foo", L"foo" );
}

if you dislike the DRY failure

#define BOTH_CHARTYPE(...) __VA_ARGS__, L __VA_ARGS__

template <typename _valueType>
void addFoo(std::basic_string<_valueType>& string_)
{
  string_ += pick( BOTH_CHARTYPE("foo") );
}

or somesuch (might need more macro magic to make the L attach to the "" properly).

This is written in c++14, but it can be adapted for c++11 relatively easily; replace Index{} with Index::value and replace decltype(auto) with a ->decltype() clause.

template<class...Ts>
struct types { using type=types; };
template<std::size_t I>
using index_t=std::integral_constant<std::size_t, I>;

template<class T, class Types>
struct type_index;
template<class T, class...Ts>
struct type_index<T, types<T, Ts...>>:index_t<0> {};
template<class T, class T0, class...Ts>
struct type_index<T, types<T0, Ts...>>:index_t<
  type_index<T, types<Ts...>>{}+1
>{};

using char_types = types<char, wchar_t, char16_t, char32_t>;

template<class T>
using char_index = type_index<T, char_types >;

#define ALL_CHAR_TYPES(...) __VA_ARGS__, L __VA_ARGS__, u __VA_ARGS__, U __VA_ARGS__

template<class T, class...Chars>
decltype(auto) pick(Chars&&...chars) {
  return dispatch( char_index< T >{}, std::forward<Chars>(chars) );
}

void addFoo( std::basic_string<_valueType>& string_ ) {
  string_ += pick<_valueType>( ALL_CHAR_TYPES("foo") );
}
Holt
3#
Holt Reply to 2018-02-13 17:03:26Z

Use an external class with specialization:

template <class>
struct basic_string_literals;

template <>
struct basic_string_literals<char> {
    constexpr char * foo = "foo";
    constexpr char delim = '.'
};

template <typename _valueType>
void addFoo(std::basic_string<_valueType>& string_)
{
    string_ += basic_string_literals<_valueType>::foo;
}

template <typename _valueType>
void foo(_valueType const delimiter_ = basic_string_literals<_valueType>::delim);

This way you don't have to specialize each function / class templated on _valueType, you only have a single point of specialization.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.35736 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO