template<int I> void div(char(*)[I % 2 == 0] = 0) {
/* this is taken when I is even */
}
template<int I> void div(char(*)[I % 2 == 1] = 0) {
/* this is taken when I is odd */
}
It can be quite useful. For example, i used it to check whether an initializer list collected using operator comma is no longer than a fixed size
The list is only accepted when M is smaller than or equal to N, which means that the initializer list has not too many elements.
The syntax char(*)[C] means: Pointer to an array with element type char and size C. If C is false (0 here), then we get the invalid type char(*)[0], pointer to a zero sized array: SFINAE makes it so that the template will be ignored then.
Expressed with boost::enable_if, that looks like this
What are good uses of SFINAE?
https://ift.tt/kbo9nXU
I like using
SFINAE
to check boolean conditions.It can be quite useful. For example, i used it to check whether an initializer list collected using operator comma is no longer than a fixed size
The list is only accepted when M is smaller than or equal to N, which means that the initializer list has not too many elements.
The syntax
char(*)[C]
means: Pointer to an array with element type char and sizeC
. IfC
is false (0 here), then we get the invalid typechar(*)[0]
, pointer to a zero sized array: SFINAE makes it so that the template will be ignored then.Expressed with
boost::enable_if
, that looks like thisIn practice, i often find the ability to check conditions a useful ability.
via Stack Overflow
September 18, 2024 at 07:08PM