模板元编程 - 尝试实现维度分析
Template metaprogramming - trying to implement Dimensional Analysis
为了更好地理解和学习,我尝试使用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
以下是一些修改:
-
无需使用
std::integral_constant
。您可以简单地使用constexpr int
s。这样可以显著简化您的mp_plus
,并且: -
mp_transform_impl
中的模板参数非常复杂。你不需要类L1
,L2
总是使用std::integer_sequence<int, ...>
。 -
使用您的
mp_transform
您无需添加typename
和::type
。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 模板元编程 - 尝试实现维度分析
- 了解算法的性能差异(如果以不同的编程语言实现)
- 为什么其中一个斐波那契序列的动态编程实现比另一个更快
- 以C++元编程风格实现 RLE 算法
- 使用多重继承实现实体组件编程
- 用C++实现多核编程中线程数的获取
- 分形编程-任何优化此代码以实现实时渲染的方法
- 如何使用套接字实现服务器客户端编程,在套接字中服务器向客户端发送消息而不从客户端获取请求
- 动态编程:棒材切割实现错误
- 使用模板元编程实现 std::all_of 的静态版本
- 基于套接字的通信独立于用于实现它的编程语言.怎么可能
- 我可以使用JSON在用不同编程语言实现的进程之间进行通信吗
- 用C/C++编程实现环回接口
- c++演示编程,易混淆的类实现
- 用C++实现Linux下的套接字编程
- 在C++表达式模板编程中是否可能实现高效的"repeatedly used intermediates"?
- 我如何编程一个双哈希实现意味着字符串
- 元编程技巧:如何简化两个元函数的实现