I also want to point out that the static_assert approach has a major drawback compared to the other ones. It prevents you to accurately detect if your function f is callable with a bool. Indeed with the static_assert you have a function that is defined and takes part in overload resolution and simply produces an error when actually compiled.
I you were using a library that, for some reason, wants to call your function with a bool if supported, else call with an int, the static_assert would fail to compile because the library would think that the function is callable with a bool and attempt to do so before encountering the static_assert. With the other 2 approach, the library would correctly detect that the function cannot be called with a bool and will instead call it with an int.
Granted, this particular situation is unlikely to happen but there is a lot of similar situation in the standard library and in other library where something like that could occur. This is why static_assert is not used much in the standard library but instead SFINAE is used (hopefully replaced by concepts in the future).
Hmm, I wasn't able to implement such detection for a free function because function template don't have a type (only concrete instantiation do).
By wrapping the function inside a struct I can showcase what I was talking about (because now I can make a template that takes my struct as a parameter). godbolt.org/z/z5a1h6
All in all maybe this is not really a problem with free function but only with member function (static or not).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Small typo at the beginning
I also want to point out that the
static_assert
approach has a major drawback compared to the other ones. It prevents you to accurately detect if your functionf
is callable with abool
. Indeed with thestatic_assert
you have a function that is defined and takes part in overload resolution and simply produces an error when actually compiled.I you were using a library that, for some reason, wants to call your function with a
bool
if supported, else call with anint
, thestatic_assert
would fail to compile because the library would think that the function is callable with abool
and attempt to do so before encountering thestatic_assert
. With the other 2 approach, the library would correctly detect that the function cannot be called with abool
and will instead call it with anint
.Granted, this particular situation is unlikely to happen but there is a lot of similar situation in the standard library and in other library where something like that could occur. This is why
static_assert
is not used much in the standard library but instead SFINAE is used (hopefully replaced by concepts in the future).Thanks, I have corrected this mistake.
I completely understand the principle. But it is not clear to me how would you write such a code. Can you provide a basic example please?
Hmm, I wasn't able to implement such detection for a free function because function template don't have a type (only concrete instantiation do).
By wrapping the function inside a
struct
I can showcase what I was talking about (because now I can make a template that takes mystruct
as a parameter).godbolt.org/z/z5a1h6
All in all maybe this is not really a problem with free function but only with member function (static or not).