1
我不知道我是否會與templateization太遠的載體,但以下問題:性病偏特::各類
我有某種容器類的。這個類可以直接取值,也可以取值爲向量。我想專門針對第二種情況。
我怎麼能......
- ...專門用於任何類型的載體類?
- ...提取專業類中的矢量參數?
代碼例如:。
// GENERAL CASE with vector of type T
template <class T>
class Container
{
std::vector<T> container;
void set(T val, int idx){
this->container[idx] = val;
}
};
// SPECIAL CASE with vector of vectors
template <>
class Container<std::vector<all types allowed>>
{
std::vector<The_type_of_vector> container;
void set(The_type_of_vector val, int idx1, int idx2){
this->container[idx1][idx2] = val; // set element idx2 in vector idx1
}
};
(當然我的容器是一個有點比這裏顯示更復雜的我還可以創建容器的兩個不同的非模板版本,但經過考慮之後,我也很好奇我怎樣才能做到這一點)
它有效,但對我來說這似乎很神奇,因爲我沒有在原始/常規模板定義中定義'element_type'。 – Michael 2014-12-02 12:45:06