假設我有一個這樣的對象:動態數據類型選擇
class Spline {
public:
Spline(std::size_t const dim);
// Quite a few functions here. One of those:
vec operator()(double const t) const; // Returns vector of dimension d
}
現在,在這個類的大多數應用中,尺寸將已經在編譯時確定,因此這將是一個很好的想法(因爲性能原因)來改變這樣的類:
template <std::size_t dim>
class Spline {
public:
Spline();
// Quite a few functions here. One of those:
vec::fixed<dim> operator()(double const t) const; // Returns vector of dimension d
}
(對於那些誰不知道,vec
和vec::fixed
是由犰狳線性代數庫定義的對象)。現在我想讓兩個版本並行運行,從而能夠在編譯時和運行時選擇維度。總之,我想創建相當於vec::fixed<dim>
的Spline::fixed<dim>
,但不實施所有功能兩次。特別是,我將不得不根據是否存在模板參數來選擇所有這些函數的返回類型。
你有什麼想法我可以做到這一點,特別是在清晰和可維護的設計方面思考? (希望我對自己說清楚,這點我不太清楚。)
不應該是'typename Spline :: fixed <10> :: type fixed_10_spline;'? – 2011-04-18 05:03:50
謝謝。我每天都會學到新的東西 - 這次你做到了;) – Thilo 2011-04-18 08:01:57