我使用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
到目前爲止好。現在我想向結構單元(或值?)添加一個包含單元表示的字符串。我wnat無論怎樣才能夠寫
cout << "v " << v.val << v.unit << endl;
,並得到類似
v 4.54545 m^1 s^-1
或
v 4.54545 m^1 kg^0 s^-1
它並不需要是美麗的,因爲這將只是檢查。並學習如何去做;)。
當然,優雅的解決方案將在編譯時評估一切。
我有一些球吧,但我不會孔/混淆你與我無結果的嘗試......
@Xeo:示例代碼是使用「user-defined-literals」和「constexpr」特性的C++ 11。爲什麼不這樣標記? – steffen
因爲問題的主題與此無關嗎? :)我可以構建一個不使用這些東西的例子。另外,如果問題的*主題*僅爲C++ 11特性,我個人認爲問題應該僅標記爲「[C++ 11]」。 – Xeo