I wonder what is the difference between using std::enable_if
as function argument vs template argument?
I have the following 2 function templates:
#include <type_traits>
template<typename T>
void f_function(T, typename std::enable_if_t<std::is_pod<T>::value, int> = 0)
{
}
template<typename T, typename = typename std::enable_if_t<std::is_pod<T>::value>>
void f_template(T)
{
}
int main()
{
int x = 1;
f_function(x);
f_template(x);
}
which produce the following assembly (as from https://godbolt.org/g/ON4Rya):
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $1, -4(%rbp)
movl -4(%rbp), %eax
movl $0, %esi
movl %eax, %edi
call void f_function<int>(int, std::enable_if<std::is_pod<int>::value, int>::type)
movl -4(%rbp), %eax
movl %eax, %edi
call void f_template<int, void>(int)
movl $0, %eax
leave
ret
void f_function<int>(int, std::enable_if<std::is_pod<int>::value, int>::type):
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
nop
popq %rbp
ret
void f_template<int, void>(int):
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
nop
popq %rbp
ret
Besides the obvious difference being that f_function
having 2 function parameters and f_template
having 2 template arguments what are the differences between them? Is there any particular use of one over another?