4

我使用 Stroustrup 在GoingNative 2012(从 23:00 分钟开始)提出的用户定义文字来玩单元实现。这是代码:

 #include <iostream>

 using std::cout;
 using std::endl;

 template<int M, int K, int S> 
 struct Unit { // a unit in the MKS system
   enum {m=M,kg=K,s=S}; 
 };

 template<typename Unit> // a magnitude with a unit
 struct Value {
   double val;
   constexpr Value(double d) : val(d) {}
 };

 using Meter = Unit<1,0,0>;
 using Second = Unit<0,0,1>;

 using Distance = Value< Meter >;
 using Time = Value< Second >;
 using Velocity = Value< Unit<1,0,-1> >;

 constexpr Value<Meter> operator "" _m(long double d)
 // a f-p literal with suffix 'm'
 {
   return Distance(d);
 }

 constexpr Value<Second> operator"" _s(long double d)
 // a f-p literal with suffix 's'
 {
   return Time(d);
 }

 constexpr Velocity operator/(Distance d, Time t)
 {
   return ( d.val / t.val );
 }

 int main(void)
 {
    Distance s = 100._m;
    Time t = 22._s;
    Velocity v = s/t;

    cout << "s " << s.val << "\nt " << t.val << endl;
    cout << "v " << v.val << endl;

   return 0;
 }

正如你所看到的,我自由地定义了一个operator/来计算速度。输出是(需要gcc-4.7):

$ g++ -std=c++0x test_units_02.cc && ./a.out
s 100
t 22
v 4.54545

到现在为止还挺好。现在我想将一个包含单位表示的字符串添加到结构单元(或值?)。无论我想怎样写

cout << "v " << v.val << v.unit << endl;

并得到类似的东西

v 4.54545 m^1 s^-1

或者

v 4.54545 m^1 kg^0 s^-1

它不需要很漂亮,因为它只是为了检查。并学习如何去做;)。

当然,优雅的解决方案是在编译时评估所有内容。

我对此进行了一些尝试,但我不会用我没有结果的尝试让您感到厌烦/混淆...

4

2 回答 2

6

首先我们添加一个unit成员Value

 template<typename Unit> // a magnitude with a unit
 struct Value {
   double val;
   constexpr static Unit unit = {};
   constexpr Value(double d) : val(d) {}
 };

然后我们写一个流输出操作符:

 template<int M, int K, int S>
 std::ostream &operator<<(std::ostream &os, Unit<M, K, S>) {
   return os << "m^" << M << " kg^" << K << " s^" << S;
 }

在编译时生成字符串是可能的,但需要constexpr编译时字符串类(例如boost::mpl::string)和十进制格式——所有这些都是可行的,但在这种情况下并不特别值得。

于 2012-11-14T14:55:00.770 回答
1

Unit已经有了我们需要的信息,所以我们可以通过添加一个函数来做到这一点Value,或者我们可以重载operator<<

template<typename U>
struct Value
{
    double val;
    constexpr Value(double d) : val(d) {}

    std::string Units() const
    {
        return "m^" + to_string(U::m) + 
               " kg^" + to_string(U::kg) +
               " s^" + to_string(U::s);
    }
};

template <typename U>
std::ostream& operator<<(std::ostream& out, Value<U> val)
{
    out << val.val
        << " m^" << U::m << " kg^" << U::kg << " s^" << U::s;
    return out;
}

我们还可以提供一个泛型operator/

template <typename U1, typename U2>
Value<Unit<U1::m - U2::m, U1::kg - U2::kg, U1::s - U2::s>>
operator/(Value<U1> v1, Value<U2> v2)
{
    return (v1.val / v2.val);
}

这给了我们更多的灵活性:

void demo()
{
    auto accel = Distance(100) / Time(22) / Time(1);
    cout << accel << endl; // Print with units.
    cout << accel.val << endl; // Print without units.
    cout << accel.Units() << endl; // Print only units.
}
于 2018-03-30T13:46:27.700 回答