When to use `static_assert` instead of SFINAE?
For one, using SFINAE may lead to another overload being picked that was originally a worse match and wouldn't be considered.
And in the situation that there are other overloads, but non of them is viable, you get some nice things like this:
#include <type_traits>
void f(int){}
void f(bool){}
void f(char){}
void f(float){}
void f(long){}
void f(double){}
void f(short){}
void f(unsigned){}
void f(void*){}
void f(void (*)()){}
template<class C, class T = int>
using EnableIf = typename std::enable_if<C::value, T>::type;
template<class T>
struct sfinae_false : std::false_type{};
template<class T>
void f(T&&, EnableIf<sfinae_false<T>> = 0){}
int main(){ struct X{}; f(X()); }
Output:
source.cpp: In function 'int main()':
source.cpp:23:30: error: no matching function for call to 'f(main()::X)'
source.cpp:23:30: note: candidates are:
source.cpp:3:6: note: void f(int)
source.cpp:3:6: note: no known conversion for argument 1 from 'main()::X' to 'int'
source.cpp:4:6: note: void f(bool)
source.cpp:4:6: note: no known conversion for argument 1 from 'main()::X' to 'bool'
source.cpp:5:6: note: void f(char)
source.cpp:5:6: note: no known conversion for argument 1 from 'main()::X' to 'char'
source.cpp:6:6: note: void f(float)
source.cpp:6:6: note: no known conversion for argument 1 from 'main()::X' to 'float'
source.cpp:7:6: note: void f(long int)
source.cpp:7:6: note: no known conversion for argument 1 from 'main()::X' to 'long int'
source.cpp:8:6: note: void f(double)
source.cpp:8:6: note: no known conversion for argument 1 from 'main()::X' to 'double'
source.cpp:9:6: note: void f(short int)
source.cpp:9:6: note: no known conversion for argument 1 from 'main()::X' to 'short int'
source.cpp:10:6: note: void f(unsigned int)
source.cpp:10:6: note: no known conversion for argument 1 from 'main()::X' to 'unsigned int'
source.cpp:11:6: note: void f(void*)
source.cpp:11:6: note: no known conversion for argument 1 from 'main()::X' to 'void*'
source.cpp:12:6: note: void f(void (*)())
source.cpp:12:6: note: no known conversion for argument 1 from 'main()::X' to 'void (*)()'
source.cpp:21:6: note: template<class T> void f(T&&, EnableIf<sfinae_false<T> >)
source.cpp:21:6: note: template argument deduction/substitution failed:
static_assert
makes the compilation fail. SFINAE allows you to remove one possible overload.
You use SFINAE, if you want another overload to be used, and static_assert
if none of them would fit such parameter.
I think static_assert
is the right choice if you want to enforce that T
is a floating point type. This method states your intent more clearly than the SFINAE solution.