我正在尝试创建一个带有boost-units的维度向量类,
//vector will be constructed vec<si::length> v(10, 1.0*si::metre);
template<typename dimension>
class vec
{
public:
//constructor setting all values to q.
vec(const size_t, const boost::units::quantity<dimension> q)
//etc
}除了operator*=和operator/=这两个执行元素级乘法和除法的函数外,其他函数都运行得很好。因为它们不会改变维度,所以它们只有在乘以/除以无量纲的数量时才有意义:我正在努力寻找一个没有锁定到特定系统中的任意无量纲的数量(例如,si或cgs单位)。
我想要这样的东西,
/** Multiply a dimensionless vector. */
vec<dimension>&
operator*=(const vec<boost::units::dimensionless_type>& b);或者可能是一些元编程魔法(我注意到boost::units::is_dimensionless存在,但我不知道如何使用它,因为我不精通一般的元编程技术)。
template<typename dimension>
template<typename a_dimensionless_type>
vec<dimension>&
vec<dimension>::operator*=(const vec<a_dimensionless_type>& b){
//some compile time check to make sure that a_dimensionless_type is actually dimensionless?
//the rest of the function
}我想要编译以下示例
vec<si::dimensionless> d(10, 2.0);
vec<si::length> l(10, 2.0*si::metre);
l*=d;
vec<cgs::dimensionless> d2(10, 2.0);
vec<cgs::length> l2(10, 2.0*cgs::centimetre);
l2*=d2;发布于 2011-02-18 23:31:51
好的,在查看了库的详细信息(并了解了BOOST_MPL_ASSERT)之后,它变得非常简单。我要向库设计者致敬。
template<typename a_dimensionless_type>
vec<dimension>&
operator*=(const vec< a_dimensionless_type >& b)
{
BOOST_MPL_ASSERT(( boost::units::is_dimensionless<boost::units::quantity<a_dimensionless_type> > ));
//the rest of the function
};发布于 2011-02-18 21:24:19
我可能误解了Boost的细节,但传统上double是无量纲的类型。
https://stackoverflow.com/questions/5040805
复制相似问题