【问题标题】:C++: Generic if statement supporting std::complex<double> AND doubleC++:支持 std::complex<double> AND double 的通用 if 语句
【发布时间】:2021-03-15 16:49:32
【问题描述】:

我们希望在 C++ 代码中启用复杂的步骤区分。为此,我们引入了一个通用模板参数template&lt;typename number&gt;,其中数字应默认设置为double,或者如果请求设置为std::complex&lt;double&gt;。在某些函数中,我们还有if 语句,它们也应该可以用复杂类型的值调用。有没有比 MWE 中下面的方法更聪明的方法来支持这两者——double 值的比较以及if 语句中std::complex&lt;double&gt; 的真实部分?非常感谢提前!

#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 概念可能会强制数字仅为doublecomplex&lt;double&gt;。 (旧的 SFINAE 方式会非常冗长:-/)
  • “有没有比下面的更聪明的方法” - 你的方法有什么问题?也许你可以直接使用std::real(),但对我来说似乎足够聪明。
  • 如果doublecomplex 的函数实现不同,你真的需要模板吗?也许你可以重载这个函数:compute(double)compute(complex)。但由于我真的不知道你想做什么,也许你真的需要模板(在这种情况下你可以使用if constexpr吗?)

标签: c++ templates operator-overloading complex-numbers


【解决方案1】:

有没有比下面的更聪明的方法 [...] 在某种意义上更聪明 (i) 我不想提取双精度值的 std::real 部分(这可以通过区分类型名来实现在 make_real (ii) 中,尽管可能会重载比较运算符以将它们也用于 double 与 std::complex 比较。

哦,是的。

如果这是个好主意,我不同意(我不喜欢在复数和浮点数之间使用operator&lt;() 的想法),但这是可能的。

您可以按如下方式添加operator&lt;()

bool operator< (std::complex<double> const & cd, double d)
 { return cd.real() < d; }

或者,如果您希望它更通用,

template <typename D>
bool operator< (std::complex<D> const & cd, D d)
 { return cd.real() < d; }

也可以

template <typename D1, typename D2>
bool operator< (std::complex<D1> const & cd, D2 d)
 { return cd.real() < d; }

以下是您修改的示例。

#include <iostream>
#include <complex>
#include <cmath>

bool operator< (std::complex<double> const & cd, double d)
 { return cd.real() < d; }

using namespace std::complex_literals;

template<typename number=double>
number
compute_number(const number& x, const number& y)
 { return x < 1.0 ? x : 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;
}

【讨论】:

  • 非常感谢@max66,这完全符合我的想法。
  • 仅重载外来类型的运算符通常是“不好的”。 std 可能会引入稍后的比较复数/双精度,从而使程序格式错误的 NDR(违反 ODR)。命名函数似乎更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多