【发布时间】:2021-07-15 08:48:51
【问题描述】:
作为一个学习练习,我试图创建一个就地计算 Hermitian 共轭的函数。当所有条目都是真实的时,它应该表现得像一个简单的转置,因此应该与 double 一起使用。我知道我可以单独专门针对双精度,并且在这个特定示例中是可行的。但是,我认为对于像 ODE 求解器这样的大型问题,专业化会变得乏味。
我尝试了以下
#include <complex>
const size_t ZERO = 0ul;
template <class value_type,
class container_type = value_type*>
auto
hermitianConjugate(container_type buffer, size_t width)
{
for (size_t row = ZERO; row < width; row++)
{
for (size_t col = ZERO; col < width; col++)
{
auto temp = std::conj(buffer[col * width + row]);
if (std::imag(temp) == 0)
{
// works for both double and std::complex
buffer[row * width + col] = buffer[col * width + row];
} else
{
// for std::complex
buffer[row * width + col] = temp;
// raises error when value_type is double
}
}
}
}
是否有不涉及显式专业化的解决方法?如果有意义的话,有什么方法可以“静态地”使用条件分支?
【问题讨论】:
-
类似
if constexpr (is_value_type_complex_v<value_type>)? (if constexpr是 C++17,没有标准特性,你必须自己编写)。 -
旁白:
template<typename container_type>就足够了吗?你不会在任何地方使用value_type
标签: c++ templates metaprogramming template-meta-programming complex-numbers