我在模板参数中使用 sizeof(),如下所示:
#include <iostream>
template<class A>
struct Foo {
A a;
};
template<class A, size_t S = sizeof(Foo<A>)>
class Bar {
public:
Bar() {
// prints 4, as expected
std::cout << sizeof(Foo<A>) << std::endl;
// prints 1! why?
std::cout << S << std::endl;
}
};
int main(int, char**) {
Bar<int> foo;
}
出于某种原因,xlc++ 决定 sizeof(Foo<A>)
因为默认的模板参数结果为 1。为什么会这样?这是编译器错误吗?它在 Visual Studio 2015 中打印 4、4。
我使用的是 xlc 版本 z/OS V2.1 XL C/C++
.
最佳答案
这可能是编译器中的错误,已在 AIX 上修复但未在 Z 上修复
在 AIX 上测试最新(即 ptf)版本 V13.1.3 预期结果 V13.1 预期结果 V12.1 预期结果 V11.1预期结果
在 ZOS 上 V2R1 失败 V2R2 失败
如果您报告错误,我们可以将修复程序放入 V2R1。
关于c++ - xlc: 模板类的 sizeof 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41689874/