我見過以下預C++ 11碼的朋友類模板,作爲一個伎倆來聲明類模板的朋友聲明通過包裝
template <typename T>
struct Wrapper
{
typedef T type;
};
template <typename T>
class Foo
{
friend class Wrapper<T>::type; // effectively makes T a friend
};
struct Test{};
int main()
{
Foo<Test> foo;
}
(在C++ 11可以簡單地用
friend T;
完成)
代碼編譯上克細++(4.9/5.1/6),但在鐺失敗++(3.5/3.6/3.7),並顯示錯誤
error: elaborated type refers to a typedef
friend class Wrapper::type;
是上述標準兼容,即,代碼有效或不?
相關:http://stackoverflow.com/questions/21952658/type-dependent-nested-name-specifier-in-elaborated-type-specifier – 0x499602D2
或http://stackoverflow.com/questions/14623338/elaborated-type-refers-to-typedef-error-when-trying-to-befriend-a-typedef –