我正在C++中使用屬性系統(C++ 11也可以),以便類可以將數據提供給腳本系統。我想提供各種屬性的「類型」,最簡單的一個控股公司價值的內部,像這樣:混合參數類型的模板重載
// Simple Property
template<typename Value>
class Property {
public:
Value get() const;
void set(Value value);
private:
Value m_value;
}
在大多數情況下,這是確定的,但有時,計算值或有被外界其他原因屬性對象。對於這樣的情況,我想提供這樣一個模板:
// Complicated Property
template<
typename Value,
typename ValueOwner,
Value (ValueOwner::*Getter)(void) const,
void (ValueOwner::*Setter)(Value)
>
class Property {
// Stuff for calling the getter & setter
}
有沒有什麼辦法可以讓這兩個模板,但不重命名其中的一個共存?我想這特殊化的getter/setter模板:
// Complicated Property
// ...
// Simple Property
template<typename Value>
class Property<Value, void, nullptr, nullptr>;
但是編譯器不一樣,並抱怨creating pointer to member function non-class type "const void"
。 Duh,當然void沒有成員函數。
然後我試圖聲明有足夠的參數模板,然後專門上面的模板,像這樣:
template<typename A, typename B, typename C, typename D>
class Property;
// Simple Property
// ...
// Complicated Property
// ...
但是,這並不能工作,因爲的getter/setter模板不指望typename作爲第三和第四個參數,但指向成員函數。
下一個嘗試:
// Complicated Property
// ...
// Simple Property
template<typename Value>
class Property<
Value,
Property<Value>,
&Property<Value>::get,
&Property<Value>::set
>;
未能編譯因爲編譯器現在不模板參數內Property<Value>
模板。
好吧,也許這一個:
// Complicated Property
// ...
template<typename Value>
struct PropertyDummy {
Value get() const;
void set(Value);
}
// Simple Property
template<typename Value>
class Property<
Value,
PropertyDummy<Value>,
&PropertyDummy<Value>::get,
&PropertyDummy<Value>::set
>;
這對於getter和setter模板參數產生template argument involves template parameter(s)
。當然他們會。
無論如何,將模板重命名爲Property
和SimpleProperty
這樣的模板對我來說已經足夠好了。但我很好奇,如果有解決方案,以便我可以寫兩個
Property<int> simpleProperty;
Property<int, MyClass, &MyClass::get, &MyClass::set> complicatedProperty;
並讓編譯器找出我指的是哪個模板。
那麼,他們不*在編譯時指定*,但它可以很好地利用內聯。我將不得不測試gcc是否足夠聰明,可以將您的解決方案與這些調用聯繫起來。無論如何,謝謝! –