c++ - 如何在 cpp 中使用 std::enable_if 处理来自模板的此类问题
问题描述
我正在尝试创建将任何元素或元素序列写入输出流的函数。我使用 std::enable_if 对模板进行操作。函数有 2 个版本:第一个用于处理序列,第二个用于处理 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:\Studying\Programming\Cpp\Repos\NiceWeather\HelloWorld\Header.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:\Studying\Programming\Cpp\Repos\NiceWeather\HelloWorld\Header.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:\Studying\Programming\Cpp\Repos\NiceWeather\HelloWorld\Header.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:\Studying\Programming\Cpp\Repos\NiceWeather\HelloWorld\Header.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
推荐阅读
- regex - .htaccess 代码用于桌面 -> 移动 | 移动 -> 桌面?
- reactjs - 从返回函数内的 onChange 事件调度时出错
- asp.net - 为什么我的 Entity Framework Core Database-First 模型自定义更改在 Re-Scaffold 后消失了?任何解决方案
- c - 通过 ./configure --host=mingw32 后 Makefile 在制作时崩溃
- azure-log-analytics - LogMetric 不适用于微软扩展框架
- c++ - 用于添加 32 位元素数组的汇编函数无法正常工作
- lightningchart - IIFE版闪电图xydata
- android - Android 谷歌地图 moveCamera 无法正常工作
- c - remove() 函数无法按预期工作
- python - 各种特征之间的标准差比较