Jon*_*Jon 3 c++ templates class stdvector c++17
我正在编写一组算法来创建一个简单的单元测试框架。用标题UnitTest为strng描述正在执行的测试的字符串的实例化名为的类。数据类型也在对象实例化时传递,这使编译器可以知道正在传递哪些数据类型。的main.cpp文件和unit_test.hpp文件显示在下面
// main.cpp file
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include "unit_test.hpp"
int main(int argc, const char * argv[]) {
std::vector<int> array_one = {1, 2, 3, 4};
std::vector<float> array_two = {0.99, 1.99, 2.99, 3.99};
std::string c ("Vector Test");
UnitTest<int, float> q(c);
double unc = 0.1;
q.vectors_are_close(array_two, array_four, unc);
return 0;
}
// unit_test.hpp file
#ifndef unit_test_hpp
#define unit_test_hpp
#endif /* unit_test_hpp */
#include <string>
#include <typeinfo>
#include <iostream>
#include <cmath>
template <class type1, class type2> class UnitTest
{
public:
unsigned long remain;
std::string str;
UnitTest(std::string strng) {
str = strng;
remain = 50 - str.length();
};
void vectors_are_close(std::vector<type1>& i, std::vector<type2>& j, double k);
// ----------------------------------------------------------------
private:
void is_close(type1& i, type2& j, double k);
};
template <class type1, class type2> void UnitTest<type1, type2>::
vectors_are_close(std::vector<type1>& i, std::vector<type2>& j, double k)
{
if (i.size() != j.size())
{
std::cout << str + std::string(remain, '.') +
std::string("FAILED") << std::endl;
}
else
{
try
{
for (int a = 0; a < i.size(); a++) {
is_close(i[a], j[a], k);
}
std::cout << str + std::string(remain, '.') +
std::string("PASSED") << std::endl;
}
catch (const char* msg)
{
std::cout << str + std::string(remain, '.') +
std::string("FAILED") << std::endl;
}
}
}
template <class type1, class type2> void UnitTest<type1, type2>::
is_close(type1& i, type2& j, double k)
{
double percent_diff = abs((j - i) / ((i + j) / 2.0));
if (percent_diff > k)
{
throw "Number not in Tolerance";
}
}
Run Code Online (Sandbox Code Playgroud)
上面显示的成员函数遍历向量容器,以确保每个归纳中的数据在一定容限内与第二个向量匹配。虽然编写的代码可以正常工作,但它要求用户每次想要使用不同的数据类型进行单元测试时,都要重新实例化该类。
在这种情况下,该类用实例化UnitTest<int, float>。但是在另一种情况下,可以使用实例化它UnitTest<float, double>。
这种方法没有什么错,但是用类似这样的实例一次实例化类UnitTest<>并让vectors_are_close()函数接受不同的数据类型似乎更优雅。有什么方法可以促进这种行为吗?
如果我理解正确,则您不想使用模板参数实例化该类,而只想使用类名称实例化该类,UnitTest并希望根据不同的type1s和type2s 传递成员函数的不同实例。
如果是这样,则不需要模板类,而是模板成员函数:
class UnitTest
{
private:
std::string str;
unsigned long remain;
public:
UnitTest(const std::string& strng)
: str{ strng },
remain{ 50 - str.size() }
{}
template <class type1, class type2>
void vectors_are_close(const std::vector<type1> &i, const std::vector<type2> &j, double k)
{
// code
}
private:
template <class type1, class type2>
void is_close(type1 i, type2 j, double k)
{
// code
}
};
int main()
{
std::vector<int> array_one{ 1, 2, 3, 4 };
std::vector<float> array_two{ 0.99f, 1.99f, 2.99f, 3.99f };
std::vector<double> array_three{ 0.99, 1.99, 2.99, 3.99 };
double unc = 0.1;
UnitTest q{ std::string{"Vector Test"} }; // non-templated class
// call different types of args to same UnitTest obj
q.vectors_are_close(array_one, array_two, unc);
q.vectors_are_close(array_one, array_three, unc);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注意:如果只想实例化整数和浮点数(或任何特殊类型的组)的成员函数,请将SFINAE与它们一起使用。
例如,以下is_oky_types特征允许您仅实例化对函数体有效的算术类型的成员函数。
#include <type_traits>
template<typename Type>
using is_oky_type = std::conjunction<
std::is_arithmetic<Type>,
std::negation<std::is_same<Type, bool>>,
std::negation<std::is_same<Type, char>>,
std::negation<std::is_same<Type, char16_t>>,
std::negation<std::is_same<Type, char32_t>>,
std::negation<std::is_same<Type, wchar_t>> >;
template<typename T, typename U>
using is_oky_types = std::conjunction<is_oky_type<T>, is_oky_type<U>>;
Run Code Online (Sandbox Code Playgroud)
并在成员函数中:
template <
class type1,
class type2,
std::enable_if_t<is_oky_types<type1, type2>::value>* = nullptr
>
void vectors_are_close(const std::vector<type1> &i, const std::vector<type2> &j, double k)
{
// code...
}
template <class type1, class type2>
void is_close(
type1 i,
type2 j,
double k,
std::enable_if_t<is_oky_types<type1, type2>::value>* = nullptr)
{
// code...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |