如何在 cpp 中使用 std::enable_if 从模板中处理此类问题

How to deal with such problems from templates using std::enable_if in cpp

本文关键字:问题 处理 if enable cpp std      更新时间:2023-10-16

我正在尝试创建将任何元素或元素序列写入输出流的函数。我使用标准::enable_if的模板来做到这一点。函数有两个版本:第一个用于处理后续,第二个用于单个元素,如 char。代码如下:

#include <iostream>
#include <list>
#include <type_traits>
#include <array>
const int maximum_array_length = 10;
const int maximum_element_length = 50;
/*below are functions which must write any element or their sequences to an any output stream*/
/*template function for non arithmetic types like char and so on.*/
template <typename T, typename O, typename std::enable_if<std::negation<typename std::is_arithmetic<T>::value>::value, bool>::type = 0>
void cstoio(T container, O& out) {
for (auto& i : container) {
if (!i.empty() && *i.begin()) {
for (auto& j : i)
if (j)
out << j;
out << 'n';
}
}
}
/*same function but for arithmetic types*/
template <typename T, typename O, typename std::enable_if<std::is_arithmetic<T>::value, bool>::type = 0>
void cstoio(T ae, O& out) {
out << ae << 'n';
}
/*test function that launchs cstoio-function 3 times with different arguments*/
void test_f() {
/*create arrays of chars*/
std::array<char, maximum_element_length> ca1{ "aaa" };
std::array<char, maximum_element_length> ca2{ "bbb" };
std::array<char, maximum_element_length> ca3{ "ccc" };
std::array<char, maximum_element_length> ca4{ "  " };
std::array<char, maximum_element_length> ca5{ " ddd" };
/*create an array of arrays of chars*/
std::array<std::array<char, maximum_element_length>, maximum_array_length> a{ ca1, ca2, ca3, ca4, ca5 };
/*create a list of strings*/
std::list<std::string> l{ "abc","", " " ,"zzz", "end" };
/*test launchs*/
cstoio(a, std::cout); //launch with an array
cstoio(l, std::cout); //launch with a list
cstoio('a', std::cout); //launch with a single char
}

但是我收到多个编译时错误:

Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'void cstoio(char,std::ostream)': cannot convert argument 1 from 'std::array<std::array<char,50>,10>' to 'char' HelloWorld  E:StudyingProgrammingCppReposNiceWeatherHelloWorldHeader.h   43  
Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'void cstoio(char,std::ostream)': cannot convert argument 1 from 'std::list<std::string,std::allocator<_Ty>>' to 'char'
with
[
_Ty=std::string
]   HelloWorld  E:StudyingProgrammingCppReposNiceWeatherHelloWorldHeader.h   44  

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0304   no instance of overloaded function "cstoio" matches the argument list   HelloWorld  E:StudyingProgrammingCppReposNiceWeatherHelloWorldHeader.h   48  
Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0304   no instance of overloaded function "cstoio" matches the argument list   HelloWorld  E:StudyingProgrammingCppReposNiceWeatherHelloWorldHeader.h   49  

您的std::negation调用是错误的。来得及

std::enable_if<std::negation<std::is_arithmetic<T>>::value, bool>::type

或者只是简单地

std::enable_if<!std::is_arithmetic<T>::value, bool>::type

演示