Your code, with necessary #include statements added as shown below,
works with MingW g++ 8.2.0 and Visual C++ 2019,
Try to create a small but complete example that reproduces the undesired
behavior.
When/if you ask here about it, if it still doesn't work, consider ALL
the points in the FAQ item "How do I post a question about code that
doesn't work correctly?", available at e.g. <url:
http://www.dietmar-kuehl.de/mirror/c++-faq/how-to-post.html#faq-5.8>.
-------------------------------------------------------------------------
#include <type_traits> // std::false_type
// template <class J>
// J f()
// {
// return J();
// }
template <class T>
struct A
{
typedef T t_type;
typedef typename T::char_type char_type; // (*)
};
template <class T,class Enable=void>
struct is_A : std::false_type
{};
template <class T>
struct is_A<T, decltype(std::declval<A<typename T::t_type>>(),void())> :
std::true_type
{};
#include <iostream>
#include <vector>
template <class J>
typename std::enable_if<is_A<J>::value,J>::type
f()
{
std::cout << "J is a specialization of A\n";
return J();
}
template <class J>
typename std::enable_if<!is_A<J>::value, J>::type
f()
{
std::cout << "J is not a specialization of A\n";
return J();
}
struct B
{
typedef char char_type;
};
auto main() -> int
{
auto j1 = f<A<B>>(); // (1)
auto j2 = f<std::vector<int>>(); // (2)
(void)j1, (void)j2;
}
-------------------------------------------------------------------------
Results:
J is a specialization of A
J is not a specialization of A
The code can IMO be expressed more cleanly, but that's a different matter.
Cheers!,
- Alf