c++ - What does it mean when one says something is SFINAE-friendly? -
i can't grasp of means when 1 mentions particular function, struct or ... sfinae-friendly.
would please explain it?
when allows substitution failure without hard error (as static_assert).
for example
template <typename t> void call_f(const t& t) { t.f(); } the function declared t, don't have f, cannot sfinae on call_f<withoutf> method exist. (demo of non compiling code).
with following change:
template <typename t> auto call_f(const t& t) ->decltype(t.f(), void()) { t.f(); } the method exists only valid t. can use sfinae as
template<typename t> auto call_f_if_available_impl(const t& t, int) -> decltype(call_f(t)) { call_f(t); } template<typename t> auto call_f_if_available_impl(const t& t, ...) { // nothing; } template<typename t> auto call_f_if_available(const t& t) { call_f_if_available_impl(t, 0); } note int = 0 , ... order overload. demo
--
an other case when template add special parameter apply sfinae specialization:
template <typename t, typename enabler = void> struct s; and then
// specialization available t respect traits. template <typename t> struct s<t, std::enable_if_t<my_type_trait<t>::value>> { };
Comments
Post a Comment