是否可以專門化在模板類中聲明的內部類?或多或少像下面的例子(它不起作用):專門研究內部類
template<unsigned N>
class Outer {
struct Inner {
typedef int Value;
};
typedef typename Inner::Value Value;
};
template<>
struct Outer<0>::Inner {
typedef float Value;
};
是否可以專門化在模板類中聲明的內部類?或多或少像下面的例子(它不起作用):專門研究內部類
template<unsigned N>
class Outer {
struct Inner {
typedef int Value;
};
typedef typename Inner::Value Value;
};
template<>
struct Outer<0>::Inner {
typedef float Value;
};
我發現最好的辦法是移出Inner
。
template<unsigned N>
struct Inner {
typedef int Value;
};
template<>
struct Inner<0> {
typedef float Value;
};
template<unsigned N>
class Outer {
typedef typename Inner<N>::Value Value;
};
它可以做到,但不像你試圖。你有專攻整個外類型:
template<>
struct Outer<0>
{
struct Inner {
typedef float Value;
};
typedef float Value;
};
謝謝,我知道這一點。我也可以將「Inner」類移動到外部並使其成爲模板,然後執行類似於'typedef typename Inner
@Giovanni Funchal:然後在外面移動Inner類,你可能從嵌套類中獲得什麼? –
我唯一能想到的就是私密。將它移動到外面使其成爲全球符號。 –
爲了等同,你需要'朋友類Inner;'''和'typedef Inner Inner;' –
@ Ben Voigt:由於標準的缺陷,內部類不被認爲是包含它的類的「朋友」。很久以前就已經解決了這個問題,一些編譯器以這種方式實現它,儘管它並不真正有效。 –
@ K-ballo:C++沒有「內部類」。這是一個嵌套類型,是的,它具有與其他成員相同的訪問權限。標準可能在遙遠的過去的某個時候所說的話幾乎沒有意義,因爲正如你所說,這個缺陷在很久以前就已經解決了。它也是*成員*,而不是*朋友*,這很重要,因爲友誼不是傳遞性的。 –