模板元编程 - 尝试实现维度分析

Template metaprogramming - trying to implement Dimensional Analysis

本文关键字:实现 编程      更新时间:2023-10-16

为了更好地理解和学习,我尝试使用boost mp11库来实现"C++模板元编程"一书中的维度分析(也可以在boost mpl库的文档中找到(。一个原因是C++11具有本书编写时不可用的功能,这些功能现在非常有用。

我正在使用Visual Studio 2019,社区版本。

一切都很顺利,直到我不得不实现运算符*。我无法执行用于 mpl 库的技巧。Operator+ 运行良好,因此问题必须与书中指出的问题相似,但应该有不同的解决方案。请帮我找到它。

(尽可能短的(示例代码如下。它被编译并正确运行。当最后 3 行未注释时,会出现错误:

错误 C2676:二进制"*":"数量<双精度,质量>"未定义此运算符或转换为预定义运算符可接受的类型

出于某种原因,运算符*是不可见的,而类似的运算符+是不可见的。

谢谢

#include <iostream>
typedef std::integer_sequence<int, 1, 0, 0, 0, 0, 0, 0> mass;
typedef std::integer_sequence<int, 0, 1, 0, 0, 0, 0, 0> length;
typedef std::integer_sequence<int, 0, 0, 1, 0, 0, 0, 0> time;
typedef std::integer_sequence<int, 0, 1, -1, 0, 0, 0, 0> velocity;
typedef std::integer_sequence<int, 0, 1, -2, 0, 0, 0, 0> acceleration;
typedef std::integer_sequence<int, 1, 1, -2, 0, 0, 0, 0> force;

template<typename T, typename D>
struct quantity
{
template<typename D1>
quantity(const quantity<T, D1>& oOther) :
m_value(oOther.m_value())
{
static_assert(std::is_same<D, D1>::value, "Type mismatch");
}
explicit quantity(T x) :
m_value(x)
{
}
T value() const
{
return m_value;
}
private:
T m_value;
};

template
<
template<class... A> class F,
class... L
>
struct mp_transform_impl;
template
<
template<class...> class F,
template<class...> class L1,
class... T1,
template<class...> class L2,
class... T2
>
struct mp_transform_impl<F, L1<T1...>, L2<T2...>>
{
using type = L1<F<T1, T2>...>;
};
template<template<class...> class F, class... L>
using mp_transform = typename mp_transform_impl<F, L...>::type;

template<class... T>
struct mp_plus_impl;
template<>
struct mp_plus_impl<>
{
using type = std::integral_constant<int, 0>;
};
template<class T1, class... T>
struct mp_plus_impl<T1, T...>
{
static constexpr auto _v = T1::value + mp_plus_impl<T...>::type::value;
using type = std::integral_constant<typename std::remove_const<decltype(_v)>::type, _v>;
};
template<class... T>
using mp_plus = typename mp_plus_impl<T...>::type;

template<typename T, typename D>
quantity<T, D> operator+(quantity<T, D> x, quantity<T, D> y)
{
return quantity<T, D>(x.value() + y.value());
}
template<typename T, typename D1, typename D2>
quantity
<
T,
typename mp_transform<mp_plus, D1, D2>::type
>
operator*(quantity<T, D1> x, quantity<T, D2> y)
{
using dim = typename mp_transform<mp_plus, D1, D2>::type;
return quantity<T, dim>(x.value() * y.value());
}

int main(int argc, char* argv[])
{
auto len1 = quantity<double, length>(4.5);
auto len2 = quantity<double, length>(3.5);
std::cout <<
"Sum: " <<
(len1 + len2).value() <<
std::endl;
auto m = quantity<double, mass>(5.0);
auto a = quantity<double, acceleration>(9.815);
// error C2676:  binary '*': 'quantity<double,mass>' does not define this operator
// or a conversion to a type acceptable to the predefined operator
//std::cout <<
//    "Product: " <<
//    (m * a).value() << std::endl;
return 0;
}

代码存在多个问题。修复了一些(但不是全部(后,这是一个工作版本:https://godbolt.org/z/3B9Sc4


以下是一些修改:

  1. 无需使用std::integral_constant。您可以简单地使用constexpr ints。这样可以显著简化您的mp_plus,并且:

  2. mp_transform_impl中的模板参数非常复杂。你不需要类L1L2总是使用std::integer_sequence<int, ...>

  3. 使用您的mp_transform您无需添加typename::type