What will the source code of this template look like?

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 

After the preliminary compilation, if we could magically see what the compiler created, we would really see:

 const int x = 24; const int y = 1; 

And will we see actual definitions for a struct Factorial multiple of it? If so, what would they look like? I am trying to wrap my head around this part of the metaprogramming process.

+6
source share
1 answer

Using g++ -fdump-tree-original in this code, I see the following result, which for this case seems to confirm your suspicion:

 ;; 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; 
+1
source

Source: https://habr.com/ru/post/945853/


All Articles