我瞭解到:nontypes模板參數
- 無類型模板參數進行一些限制。一般來說,它們可以是常量整數值(包括枚舉)或指向具有外部鏈接的對象的指針。
所以我做了下面的代碼
template <char const* name>
class MyClass {
…
};
char const* s = "hello";
MyClass<s> x; // ERROR:
此代碼沒有工作,產生錯誤's' is not a valid template argument
我的第二個代碼也沒有工作
2.
template <char const* name>
class MyClass {
…
};
extern char const *s = "hello";
MyClass<s> x; //error 's' is not a valid template argument`
但奇怪的是這個代碼是罰款
template <char const* name>
class MyClass {
…
};
extern char const s[] = "hello";
MyClass<s> x; // OK
請告訴什麼是在所有這三個碼的發生?
也告訴如何糾正錯誤,使其他兩個代碼也工作。
啊,舊的'const *'vs'* const'栗子... – 2012-02-07 08:20:10