1
template <int parameter> class MyClass
是上面的模板專業化嗎?我不這麼認爲,但我不確定,我不知道模板可以接收參數作爲函數..他們的參數存儲在哪裏?這是一個模板專精?
template <int parameter> class MyClass
是上面的模板專業化嗎?我不這麼認爲,但我不確定,我不知道模板可以接收參數作爲函數..他們的參數存儲在哪裏?這是一個模板專精?
模板參數不一定需要是類型名稱:它們也可以是數字。例如,std::array
採用數組大小爲size_t
的參數。
在你的情況下,類模板採用類型爲int
的參數,這是完全正確的。下面是一個如何使用這樣的參數的例子:
template <int param> struct MyClass {
int array[param]; // param is a compile-time constant.
};
int main() {
MyClass<5> m;
m.array[3] = 8; // indexes 0..4 are allowed.
return 0;
}
它們的參數存儲在它們的類型信息中。
不,這不是模板專業化。看看這個:
template <int, int> class MyClass; // <-- primary template
template <int> class MyClass<int, 4>; // <-- partial specialization
template <> class MyClass<5, 4>; // <-- specialization
參數存儲在哪裏?在RAM中。編譯期間。 – 2013-03-02 09:25:36