示例代码
#include <iostream>
struct base {};
template<typename Type>
struct left : base {
Type value;
};
template<typename Type>
struct right : base {
Type value;
};
int main() {
std::cout << "sizeof left<base> = " << sizeof(left<base>) << std::endl;
std::cout << "sizeof left<right<base>> = " << sizeof(left<right<base>>) << std::endl;
std::cout << "sizeof left<right<left<right<left<base>>>>> = " << sizeof(left<right<left<right<left<base>>>>>) << std::endl;
}
输出
GCC 4.6 是
sizeof left<base> = 2
sizeof left<right<base>> = 3
sizeof left<right<left<right<left<base>>>>> = 6
用铿锵3.1
sizeof left<base> = 2
sizeof left<right<base>> = 3
sizeof left<right<left<right<left<base>>>>> = 6
使用 MSVC 2012
sizeof left<base> = 1
sizeof left<right<base>> = 1
sizeof left<right<left<right<left<base>>>>> = 1
所以,问题是,它是 GCC/clang 中的错误,还是实现定义,还是正确的输出(来自标准的引用,或者对这种行为的解释会很好)