As a continuation of my previous question , I am trying to discover the existence of a template function that requires explicit specialization.
My current working code detects functions without a pattern (thanks to the help of DyP) if they take at least one parameter so that you can search for a dependent search by name:
// switch to 0 to test the other case #define ENABLE_FOO_BAR 1 namespace foo { #if ENABLE_FOO_BAR int bar(int); #endif } namespace feature_test { namespace detail { using namespace foo; template<typename T> decltype(bar(std::declval<T>())) test(int); template<typename> void test(...); } static constexpr bool has_foo_bar = std::is_same<decltype(detail::test<int>(0)), int>::value; static_assert(has_foo_bar == ENABLE_FOO_BAR, "something went wrong"); }
(the macro ENABLE_FOO_BAR is only for testing, in my real code I do not have such a macro, otherwise I would not use SFINAE)
This also works great with template functions when their template arguments can be automatically output by the compiler:
namespace foo { #if ENABLE_FOO_BAR template<typename T> int bar(T); #endif }
However, when I try to find a template function that requires explicit specialization, static_assert kicks when foo::bar() exists :
namespace foo { #if ENABLE_FOO_BAR template<typename T, typename U> T bar(U); #endif } //... // error: static assertion failed: something went wrong
Obviously, the compiler cannot output the arguments to the bar() template, so detection is not performed. I tried to fix this by explicitly highlighting the call:
template<typename T> decltype(bar<int, T>(std::declval<T>())) test(int); // explicit specialization ^^^^^^^^
This works fine when foo::bar() exists (the function is correctly detected), but now all hell breaks when foo::bar() does not exist :
error: 'bar' was not declared in this scope template<typename T> decltype(bar<int, T>(std::declval<T>())) test(int); ^ error: expected primary-expression before 'int' template<typename T> decltype(bar<int, T>(std::declval<T>())) test(int); ^ // lots of meaningless errors that derive from the first two
It seems my attempt to explicitly specialize failed because the compiler does not know that bar is a template.
I will spare you everything that I tried to fix, and let's get straight to the point: how can I detect the existence of such a function as template<typename T, typename U> T bar(U); that requires explicit specialization to instantiate?