[英]Issue with enable_if and multiple conditions
我试图实现一个将泛型类型转换为字符串的函数。 积分类型需要使用std::to_string()
,字符串和字符使用std::string()
和逐个元素的元素转换为使用其他方法之一的字符串(取决于它们的内容)。
这就是我所拥有的:
//Arithmetic types
template<class T>
typename std::enable_if<std::is_arithmetic<T>::value, std::string>::type convertToString(const T& t){
return std::to_string(t);
}
//Other types using string ctor
template<class T>
typename std::enable_if<std::__and_<std::__not_<std::is_arithmetic<T>>::type,
std::__not_<std::is_same<T, <T,
std::vector<typename T::value_type, typename T::allocator_type>>::value
>>>::value, std::string>::type convertToString(const T& t){
return std::string(t);
}
//Vectors
template<class T>
typename std::enable_if<std::is_same<T, std::vector<typename T::value_type,
typename T::allocator_type>>::value, std::string>::type convertToString(const T& t){
std::string str;
for(std::size_t i = 0; i < t.size(); i++){
str += convertToString(t[i]);
}
return str;
}
问题是第二个函数没有编译。 如何设计第二个函数以便它编译(和工作)并且不会产生歧义问题?
Oktalist的答案解释了为什么你的类型特征不能编译。 此外,您不应该使用__and_
和__not_
。 这些是保留的,可以在下一个编译器版本中轻松更改。 实现这些特征的自己版本很容易(例如,参见可能的conjunction
实现)。
我建议采用完全不同的方法。 我们可以使用choice<>
来使这些情况重载更简单:
template <int I> struct choice : choice<I+1> { };
template <> struct choice<10> { };
通过:
// arithmetic version
template <class T>
auto convertToStringHelper(T const& t, choice<0> )
-> decltype(std::to_string(t))
{
return std::to_string(t);
}
// non-arithmetic version
template <class T>
auto convertToStringHelper(T const& t, choice<1> )
-> decltype(std::string(t))
{
return std::string(t);
}
// vector version
template <class T, class A>
std::string convertToStringHelper(std::vector<T,A> const& v, choice<2> )
{
// implementation here
}
template <class T>
std::string convertToString(T const& t) {
return convertToStringHelper(t, choice<0>{});
}
这很好,因为你得到的所有SFINAE都没有任何enable_if
cruft。
一种可能的方法是添加is_vector trait( 在此处查看更多详细信息):
template<typename T> struct is_vector : public std::false_type {};
template<typename T, typename A>
struct is_vector<std::vector<T, A>> : public std::true_type {};
然后修改convertToString函数模板,如下所示:
// Arithmetic types
template<class T>
typename std::enable_if<std::is_arithmetic<T>::value, std::string>::type convertToString(const T& t) {
return std::to_string(t);
}
// Other types using string ctor
template<class T>
typename std::enable_if<!std::is_arithmetic<T>::value && !is_vector<T>::value, std::string>::type convertToString(const T& t) {
return std::string(t);
}
// Vectors
template<class T>
typename std::enable_if<!std::is_arithmetic<T>::value && is_vector<T>::value, std::string>::type convertToString(const T& t) {
std::string str;
for(std::size_t i = 0; i < t.size(); i++){
str += convertToString(t[i]);
}
return str;
}
标记错误的模板:
template<class T>
typename std::enable_if<std::__and_<std::__not_<std::is_arithmetic<T>>::type,
// ^^^^^^[1]
std::__not_<std::is_same<T, <T,
// ^^^[2]
std::vector<typename T::value_type, typename T::allocator_type>>::value
// ^^^^^^^[3]
>>>::value, std::string>::type convertToString(const T& t){
// ^[4]
return std::string(t);
}
// [1] nested ::type not needed and ill-formed without typename keyword
// [2] <T, is garbage
// [3] nested ::value ill-formed because std::__not_ argument must be a type
// [4] too many closing angle brackets
修复了错误的模板:
template<class T>
typename std::enable_if<std::__and_<std::__not_<std::is_arithmetic<T>>,
std::__not_<std::is_same<T,
std::vector<typename T::value_type, typename T::allocator_type>>
>>::value, std::string>::type convertToString(const T& t){
return std::string(t);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.