I was experimenting with some template programming on the compiler explorer when I noticed that MSVC left a lot of templated function definitions in the assembly output even though they were optimized out of the main function and not called at any point; this is in contrast to GCC which does not include them. I checked using my installation of Visual Studio 2019 (16.1.3) and the output is the same as the compiler explorer's.
Are there any combinations of compiler and linker flags, or directives I can give in the source, which will remove these from the binary? I have tried combinations involving /O1 but to no avail.
The code I used is
template <unsigned int N>
constexpr unsigned int fact()
{
return N * fact<N-1>();
}
template <>
constexpr unsigned int fact<0>()
{
return 1;
}
int main()
{
return fact<4>();
}
The GCC output is
main:
mov eax, 24
ret
and from MSVC
unsigned int fact<1>(void) PROC ; fact<1>, COMDAT
mov eax, 1
ret 0
unsigned int fact<1>(void) ENDP ; fact<1>
etc...
unsigned int fact<4>(void) PROC ; fact<4>, COMDAT
mov eax, 24
ret 0
unsigned int fact<4>(void) ENDP ; fact<4>
unsigned int fact<0>(void) PROC ; fact<0>, COMDAT
mov eax, 1
ret 0
unsigned int fact<0>(void) ENDP ; fact<0>
main PROC ; COMDAT
mov eax, 24
ret 0
main ENDP
whereas I am hoping to find a way for it to simply emit
main PROC ; COMDAT
mov eax, 24
ret 0
main ENDP