为了更好地理解和学习,我尝试使用boost mp11库来实现“C++模板元编程”一书中的维度分析(也可以在boost mpl库的文档中找到)。 一个原因是C++11有一些在编写这本书时没有的特性,这些特性现在非常有用。
我使用的是Visual Studio 2019社区版。
一切都很顺利,直到我不得不实现运算符*。 我不能执行用于mpl库的技巧。 运算符+工作得很好,所以问题一定和书中指出的类似,但应该有一个不同的解决方案。 请帮我找到它。
下面是(尽可能短的)示例代码。 它得到编译并正确运行。 当最后3行被取消注释时,会出现以下错误:
错误C2676:二进制“*”:“quantity<;double,mass>;” 不定义此运算符或到预定义运算符可接受的类型的转换
由于某种原因,运算符*是不可见的,而类似的运算符+是不可见的。
谢谢
#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
中的模板参数非常复杂,没有必要。 当总是使用std::integer_sequence
时,您不需要类L1
,L2
。
使用mp_transform
,您不需要添加typename
和::type
。