的代碼錯誤路線參考如下:的Visual C++編譯器給出了模棱兩可的符號
namespace n1
{
template <class T>
void n2();
template <class T>
void n2(T);
}
namespace n2 /* line 12 */
{
class c {};
}
using namespace n1;
namespace n3
{
void foo(n2::c);
}
void n3::foo(n2::c) /* line 24 */
{
}
當嘗試使用最新版本的Visual C編譯它++我得到以下錯誤:
1>test.cpp(24): error C2872: 'n2': ambiguous symbol
1>test.cpp(12): note: could be 'n2'
1>test.cpp(24): note: or 'n2'
第12行和第24行在前面的代碼段中用註釋標記。
這是怎麼回事?如果我刪除了foo的定義並在函數參數列表外部聲明瞭一個類型爲n2 :: c的變量,那麼它編譯得很好,我想這是因爲編譯器認爲我引用的是類而不是任何模板函數。同樣,如果我刪除命名空間n1內的兩個n2模板函數的第二個定義,那麼編譯器會給我一個錯誤消息,但引用正確的行:第12行和將n2定義爲函數的行(而不是第24行)。這是一個編譯器錯誤?
我已經testet這個代碼與各種編譯: VC++ 2015年不編譯並輸出錯誤的行號。 VC++ 2012和2013不編譯,但根本不輸出行號(候選人)。 gcc(各種版本)做編譯。 - 行號是錯誤的bug(似乎是VC++ 2015中的一個新功能),但我無法確定此代碼是否應該編譯。 –
這裏沒有人可以爲你改變這一點。報告connect.microsoft.com上的錯誤和不需要的編譯器行爲 –