Poeta Kodu
Poeta Kodu

Reputation: 1150

Use proper string literal in templated function in C++

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_ = '.');

Upvotes: 4

Views: 778

Answers (2)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

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 , but it can be adapted for 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") );
}

Upvotes: 2

Holt
Holt

Reputation: 37606

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.

Upvotes: 2

Related Questions