template <int N>
struct Factorial {
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0> {
enum { value = 1 };
};
const int x = Factorial<4>::value; // == 24
const int y = Factorial<0>::value; // == 1
Run Code Online (Sandbox Code Playgroud)
在预编译之后,如果我们能够神奇地看到编译器生成的内容,我们实际上会看到:
const int x = 24;
const int y = 1;
Run Code Online (Sandbox Code Playgroud)
我们会看到其中struct Factorial
多个的实际定义吗?如果是这样,他们会怎么样?我正试着围绕元编程过程的这一部分.
使用g++ -fdump-tree-original
此代码,我看到以下结果,对于本例来说,这似乎证实了您的怀疑:
;; Function int main() (null)
;; enabled by -tree-original
{
const int x = 24;
const int y = 1;
<<cleanup_point const int x = 24;>>;
<<cleanup_point const int y = 1;>>;
}
return <retval> = 0;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
108 次 |
最近记录: |