在 C++ 的模板化函数中使用正确的字符串文字 [英] Use proper string literal in templated function in C++
问题描述
我有一个函数,它被模板化以匹配每个 std::basic_string 实例化:
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";
}
我希望 "foo"
是 _valueType
依赖的文字.我不想使用专业化,因为在实际项目中,我对整个类进行了模板化,这将是很多工作.
我目前在函数体中使用 if constexpr
,但问题开始了,当我有这样的函数采用默认参数时:
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_ = '.');
推荐答案
我假设你只支持 char
和 wchar_t
.如果您想支持更长的枚举类型列表,您也可以这样做.这不适用于非本地枚举的类型列表.
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.
现在在你的类中定义:
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 失败
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") );
}
或类似的(可能需要更多的宏魔法才能使 L
正确附加到 ""
).
or somesuch (might need more macro magic to make the L
attach to the ""
properly).
这是写在 c++14,但它可以适用于 c++11 相对容易;将 Index{}
替换为 Index::value
并将 decltype(auto)
替换为 ->decltype()
> 子句.
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") );
}
这篇关于在 C++ 的模板化函数中使用正确的字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!