2

我正在尝试学习如何将 c++11 用户定义的文字用于物理属性单位。问题是,我如何避免这些单位的混合。所以 (8.0_kg + 8.0_km)--> 给出错误。有什么想法吗?我是 C++ 新手,请善待。

class Mass{
    public:        
        //Mass(){
        //    cout << "only Mass units allowed in here" << endl;
        //}
        //~Mass();
        long double getWeight(long double a);        
        double car, house, cat;

    private:
        long double a;


    };


long double Mass::getWeight(long double w) {

    cout << "returning argument: " << w << '\n'<< endl;
    return 0;
}


long double operator"" _km(long double d) { return d * 1000.0; }
long double operator"" _m (long double d) {return d;}
long double operator"" _cm(long double d) { return d / 100.0; }

long double operator"" _tonne(long double m) { return m * 1000.0 ; }
long double operator"" _kg(long double m) { return m ; }
long double operator"" _lb(long double m) { return m * 0.453592; }

long double getDistance(long double d){
    long double starting_d = 61.0_kg;
    long double total_d = d + starting_d;
    cout << "the distance I have run is: " << total_d << endl;
    return 0;
}

int main() {


    cout <<  6.0_km << endl;
    cout <<  6.0_km + 3.0_m << endl;
    cout <<  6.0_km + 3.0_m + 15.0_cm << '\n' << endl;

    cout <<  8.0_tonne << endl;
    cout <<  8.0_km + 4.0_kg << endl;
    cout <<  8.0_km + 4.0_kg + 21.0_lb << '\n' << endl;


    long double distance = 5.45_km;
    getDistance(distance);

    Mass obj1;
    obj1.getWeight(13.96_lb);

    cout << "This is clearly wrong:  "<<  8.0_km + 4.0_kg << endl;

    obj1.getWeight(10.96_km); // so is this


}
4

2 回答 2

8

您需要定义自己的类型,因为您不能限制原语表示的内容。

您可以使用“标记模板” 1来避免运算符等的重复并保持其类型安全。
这可以扩展,以便您获得例如distance * distance = areaspeed * time = distance由编译器检查。

这是一个简短的示例:

template<typename Kind>
struct Value
{
    long double value;
    Value& operator+= (Value v) { value += v.value; return *this; }
};

template <typename Kind>
Value<Kind> operator+ (Value<Kind> lhs, Value<Kind> rhs) { return lhs += rhs; }

// These types don't need definitions; we only need some unique type names.
struct M;
struct D;

using Mass = Value<M>;
using Distance = Value<D>;

Mass operator"" _kg(long double d) { return { d };}
Mass operator"" _lb(long double d) { return { d * 0.453592 };}

Distance operator"" _km(long double d) { return { d * 1000 };}
Distance operator"" _mile(long double d) { return { d * 1609 };}

int main()
{
    // OK
    Distance d = 1.2_km + 0.2_mile;
    // OK
    Mass m = 2.3_kg + 1.4_lb;      
    // invalid operands to binary expression ('Distance' (aka 'Value<D>')
    // and 'Mass' (aka 'Value<M>'))
    Distance d2 = 2.4_km + 1.2_kg; // Nope
}

1)我认为 C++ 中没有一个既定的术语,但它与 Haskell 所指的phantom types非常相似。

于 2017-03-06T13:18:47.143 回答
2

创建表示不同单位数值的类。早在 C++ 11 之前,它就是这样完成的。

不过,自定义文字可以使实例化更具可读性,因为它有助于保留通常的数字和单位顺序:)

请参阅http://en.cppreference.com/w/cpp/language/user_literal

class MassKg
{
    double value;

    // public c'tor, numeric operators, &c.
};

// ...

MassKg mass(5.0);
DistanceM distance(3.0);
auto c = mass * distance; // may yield an instance of TorqueKgM, or MomentumKgM, therefore
// explicit functions / methods are preferrable for mixed
// multiplication or division
auto mass2 = mass + MassKg(2.0); // yiels an instance of MassKg
auto invalid = mass + distance; // compile time error
于 2017-03-06T13:04:30.393 回答