考慮以下幾點:模板別名能見度嵌套類
template<typename X>
struct Z {};
struct A
{
using Z = ::Z<int>;
struct B : Z
{
using C = Z;
};
};
編譯沒有問題。尼斯。但是,現在在Z
添加另一個參數:
template<typename X, typename Y>
struct Z {};
struct A
{
template<typename X>
using Z = ::Z<X, int>;
struct B : Z<B>
{
using C = Z<B>; // error: too few template arguments for class template 'Z'
};
};
好吧,也許這是有道理的,派生的嵌套類B
當A
類模板別名Z
的定義是可見的,而不是它的體內,引發由於錯誤全局定義Z
有兩個參數。
但是爲什麼在第一種情況下的行爲不同,當Z
只是A
中的一種類型別名?
最後,請A
模板:
template<typename X, typename Y>
struct Z {};
template<typename T>
struct A
{
template<typename X>
using Z = ::Z<X, int>;
struct B : Z<B>
{
using C = Z<B>;
};
};
現在錯誤消失。 爲什麼?
(測試鏘3.6和GCC 4.9.2)
哇。這很清楚,謝謝。當'A'停止作爲模板時,實際上出現了這個錯誤,我認爲這會簡化代碼。然而,我現在被迫爲兩個'Z'使用兩個不同的名字,這隻會使代碼更醜。如果有更好的解決方法,請告訴我。 – iavr
@iavr如何使用C = Z;'? (不適用於'A'作爲模板) – Columbo
現在令人印象深刻:-)是的,它適用於此簡化代碼,但不適用於我的原始代碼('未知類型名稱'Z'')。我將不得不檢查其中的差異。 'A'不再是一個模板,我打算保持這種模式。 – iavr