SFINAE does not work with functions without a template (member or non-member).
As Kerrek SB points out, creating their non-member function templates will work. Or, as Xeo points out, creating member function templates with a default template argument will also work.
However, this only works because the two conditions std::enable_if do not overlap . If you want to add another overload for int (say), you will find that it does not scale as well. Depending on what you want to do, tag dispatching usually scales better than SFINAE with a few alternatives you want to send:
#include<type_traits> template<typename Ret> class Foo { public: void _on_dispatched() { // tag dispachting: create dummy of either std::false_type or std::true_type // almost guaranteed to be optimized away by a decent compiler helper_on_dispatched(std::is_void<Ret>()); } private: void helper_on_dispatched(std::false_type) { // do stuff for non-void } void helper_on_dispatched(std::true_type) { // do stuff for void } }; int main() { Foo<void>()._on_dispatched(); Foo<int>()._on_dispatched(); return 0; }
source share