【发布时间】:2021-12-28 20:14:02
【问题描述】:
我有下面的课程,但它只适用于浮点数。如何也添加整数?那是多个需要语句?或者有什么可以包含所有数字类型的东西?
或者有更好的方法吗?
#ifndef COMPLEX_H
#define COMPLEX_H
#include <concepts>
#include <iostream>
template <class T>
requires std::floating_point<T> // How to add integral, signed integral, etc
class Complex {
private:
T re = 0;
T im = 0;
public:
Complex() {
std::cout << "Complex: Default constructor" << std::endl;
};
Complex(T real) : re{re} {
std::cout << "Complex: Constructing from assignement!" << std::endl;
};
bool operator<(const Complex<T>& other) {
return re < other.re && im < other.im;
}
};
#endif // COMPLEX_H
【问题讨论】:
标签: c++ c++20 c++-concepts