Igo*_*kov 12 c++ gcc templates variadic c++11
以下结构意味着什么?
#include <iostream>
template <int ...> struct s;
int main() {
int i = s<,>::xxx;
std::cout << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它由gcc 4.4.5+编译并在执行时输出0.
Mag*_*off 13
我重写了这个程序:
template <int ...> struct s;
int main() {
int i = s<,>::xxx;
return i;
}
Run Code Online (Sandbox Code Playgroud)
并使用-S-switch 编译它,以获得我清理到以下内容的程序集输出:
main:
pushq %rbp
movq %rsp, %rbp
movl -4(%rbp), %eax
popq %rbp
ret
Run Code Online (Sandbox Code Playgroud)
现在,我的asm有点生疏,但唯一重要的代码似乎是movl -4(%rbp), %eax,它将返回值设置为它可以读取的内容i.换句话说,当输入main函数时,程序只返回堆栈顶部的任何内容.这似乎证实了@ jrok的评论,即初始化在i某种程度上被忽略了.没有为s<,>::xxxmystical -expression 生成代码.
底线; 这看起来像编译器错误.编译器应该给出错误消息.
确凿的旁注:我为程序获得了相同的汇编输出int main() { int i; return i; }.