【发布时间】:2021-03-15 16:49:32
【问题描述】:
我们希望在 C++ 代码中启用复杂的步骤区分。为此,我们引入了一个通用模板参数template<typename number>,其中数字应默认设置为double,或者如果请求设置为std::complex<double>。在某些函数中,我们还有if 语句,它们也应该可以用复杂类型的值调用。有没有比 MWE 中下面的方法更聪明的方法来支持这两者——double 值的比较以及if 语句中std::complex<double> 的真实部分?非常感谢提前!
#include <iostream>
#include <complex>
#include <cmath>
using namespace std::complex_literals;
template<typename number>
double make_real(const number& value)
{
return std::real(value);
}
template<typename number=double>
number
compute_number(const number& x, const number& y)
{
if (make_real<number>(x) < 1.0)
return x;
else
return y;
}
int main()
{
std::cout << "Evaluate function with double" << std::endl;
std::cout << compute_number(3.0, 4.0) << std::endl;
std::cout << "Evaluate function with complex number" << std::endl;
std::cout << compute_number(3.0+2i, 4.0-2i) << std::endl;
return 0;
}
编辑:我基于 C++20 概念和函数重载改进了我的代码。我认为可能无法避免 if 语句中的make_real 调用,因为std::complex 不支持任何比较操作。
#include <iostream>
#include <complex>
#include <cmath>
using namespace std::complex_literals;
double make_real(const std::complex<double>& value)
{
return std::real(value);
}
double make_real(const double& value)
{
return value;
}
template <typename T>
struct is_complex_floating_point : public std::false_type {};
template <typename T>
struct is_complex_floating_point<std::complex<T>>
: public std::bool_constant<std::is_floating_point_v<T>>
{};
template <typename T>
concept real_or_complex_floating_point =
std::floating_point<T> ||
is_complex_floating_point<std::remove_const_t<T>>::value;
template<real_or_complex_floating_point number>
number
compute_number(const number& x, const number& y)
{
if (make_real(x) < 1.0) // is there a way to avoid the make_real call?
return x;
else
return y;
}
int main()
{
std::cout << "Evaluate function with double" << std::endl;
std::cout << compute_number(3.0, 4.0) << std::endl;
std::cout << "Evaluate function with complex number" << std::endl;
std::cout << compute_number(3.0+2i, 4.0-2i) << std::endl;
return 0;
}
【问题讨论】:
-
在哪些方面更智能?
-
typename number=double中的默认值是没有用的,因为推导出了number。 -
C++20 概念可能会强制数字仅为
double或complex<double>。 (旧的 SFINAE 方式会非常冗长:-/) -
“有没有比下面的更聪明的方法” - 你的方法有什么问题?也许你可以直接使用
std::real(),但对我来说似乎足够聪明。 -
如果
double和complex的函数实现不同,你真的需要模板吗?也许你可以重载这个函数:compute(double)和compute(complex)。但由于我真的不知道你想做什么,也许你真的需要模板(在这种情况下你可以使用if constexpr吗?)
标签: c++ templates operator-overloading complex-numbers