Re: type function U[1] and U(*)[1] SFINAE issue
On May 24, 5:10 am, Fei Liu <fei....@gmail.com> wrote:
Hello, I just hit a strange problem regarding SFINAE. The following code
causes compile error (void cannot be array element type), I thought
SFINA should match test(...) version instead and not issue any error.
If I replace U[1] with U(*)[1], then the code compiles again. I couldn't
make the sense out of it. What's the magic with (*)? Note that
function<int>::yes returns correct result 0 even with U[1].
What compiler are you using?
as long as U is a function type, U[1] is illformed, array of function
is not allowed.
when you say "U[1]" as a function parameter type, it's deprecated as
"U*"
when you say "U(*)[1]", it's a pointer to array of U.
Please help me out.
Fei
#include <iostream>
using namespace std;
template <typename T>
struct function{
template <typename U> static char test(...);
template <typename U> static char (&test(U[1]))[2];
enum { yes = (sizeof(function<T>::test<T>(0)) == 1) };
};
could you explain your algorithm here?
int main(){
cout << "int: " << function<int>::ye=
s << endl;
cout << "void: " << function<void>::yes=
<< endl;
// function
cout << "void(): " << function<void()>::yes=
<< endl;
// function ptr
cout << "void(*)(): " << function<void(*)()>::ye=
s << endl;
}
a clean implementation of is_function from this NG a while ago:
by means of "function to pointer-to-function conversion"
typedef char (&yes) [1];
typedef char (&no) [2];
// is_class_type
template <typename T>
class is_class_type
{
template <typename S>
static yes check(int S::*);
template <typename S>
static no check(...);
public:
static bool const value = (sizeof(check<T>(0)) == sizeof(yes));
};
template <typename T>
class is_function
{
template <typename S>
static yes check(S*);
template <typename S>
static no check(...);
public:
static bool const value =
(!is_class_type<T>::value)
&&
(sizeof(check<T>(*(T*)(0))) == sizeof(yes));
};
__
Best Regards
Barry