在 C++ 的模板函数中使用正确的字符串文字

Use proper string literal in templated function in C++

我有一个函数,它被模板化以匹配每个 std::basic_string 实例化:

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

我希望 "foo" 成为 _valueType 依赖文字。我不想使用专业化,因为在实际项目中,我将整个 class 模板化,这将是很多工作。
我目前在函数体中使用 if constexpr,但是问题开始了,当我有函数采用这样的默认参数时:

template <typename _valueType>
void foo(_valueType const delimiter_ = '.');

使用具有专业化的外部 class:

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);

这样您就不必专门化每个函数/class 模板化在 _valueType 上,您只有一个专门化点。

我假设您只支持 charwchar_t。如果你想支持更长的枚举类型列表,你也可以这样做。这不适用于非本地枚举类型列表。

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

这是一个简洁的小帮手,可让您在编译时分派任意数量的参数。

现在在你的 class 中定义:

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) );
}

您现在可以这样做:

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

如果您不喜欢 DRY 失败

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

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

或类似的东西(可能需要更多的宏魔法才能使 L 正确附加到 "")。

这个写在, but it can be adapted for 比较容易;将 Index{} 替换为 Index::value,并将 decltype(auto) 替换为 ->decltype() 子句。

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") );
}