我們在follwing源代碼的編譯中觀察到的奇怪的行爲:作爲模板模板參數的C++ 11模板別名導致不同的類型?
template<template<class> class TT> struct X { };
template<class> struct Y { };
template<class T> using Z = Y<T>;
int main() {
X<Y> y;
X<Z> z;
z = y; // it fails here
}
這是從C++ 11標準提案爲模板別名截取的略微變形例:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf(參見第4頁) 還要注意提案「聲明y和z是同一類型的。」因此,在我們的解釋中,應該可以從y分配(或複製構造)z。
但是,這段代碼不能用gcc 4.8.1和clang 3.3編譯。這是編譯器中的錯誤還是我們誤解了標準?
由於提前, craffael等人)
P.S.鐺鐺錯誤消息是:
error: no viable overloaded '='
note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'X<template Y>' to 'const X<template Z>' for 1st argument
template<template<class> class TT> struct X { };
note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'X<template Y>' to 'X<template Z>' for 1st argument
template<template<class> class TT> struct X { };
'Y'和'Z'是不同的* template-names *,因此它們會產生與'X'不同的實例。見§14.5.7/ 1。 – Xeo