如何在 cpp 中使用 std::enable_if 从模板中处理此类问题
How to deal with such problems from templates using std::enable_if in cpp
我正在尝试创建将任何元素或元素序列写入输出流的函数。我使用标准::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
演示
相关文章:
- 警告处理为错误这里有什么问题
- posix_spawn():使用posix_scawn()时的错误处理问题
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- C++:处理抽象类中的错误时出现问题
- 在通知提升间处理条件变量时未按住锁会导致问题
- 如何在 cpp 中使用 std::enable_if 从模板中处理此类问题
- wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
- 关于 istream 中的错误处理的问题
- 有关如何处理 vulkan 队列系列的问题
- 我如何处理这种情况C++大小问题
- 在C++中做一个场景问题,我不确定如何处理布尔和if循环
- 用wchar_t处理 unicode 字符好吗?它不会引起任何问题吗?
- 关于 ocilib 错误处理的问题,如何使用 ocilib 正确捕获日志错误?
- Eclipse 问题 - 编译期间不考虑 .c 和 .cpp 文件中定义的预处理
- STD :: FSTREAM文件处理重复使用的问题
- 在 cplex c++ 音乐会 api++ 中处理非常大的问题而不会耗尽内存
- 使用 ARDUINO 和处理在 8x8 LED 矩阵上显示数据时出现问题
- googlemock - 如何处理"多重定义"问题?
- 指纹读取器 - 从 C# 代码创建事件处理程序到C++的问题
- 问题处理缺点引用另一个类中的类