Re: Factoring SFINAE tests
Gianni Mariani wrote:
Below is an attempt to factorize the "SFINAE" idiom. I have tried it on
three compilers and they all complain. As far as I can tell, it's valid
code. The questions are, are the compilers right to reject the code or
is the code right and why ? and has anyone successfully mastered the act
of factorizing SFINAE?
Well, I'd rather ask than answer
:-)
----------------------------------------------------------------------
template <template <typename> class w_Test, typename w_Type>
class sfinae_test
{
typedef char (&false_value)[1];
typedef char (&true_value)[2];
struct base {};
struct derived : base {};
static derived & make_derived();
static w_Type& make_test();
template <bool i>
struct H
{
};
H is not used, right?
template <typename Z>
static false_value func( Z &, base & );
template <typename Z>
static true_value func(Z&, derived&, typename w_Test<Z>::type* =0 );
what good made by replacing ... with these two /func/s with base& and
derived&, and the dummy placehoder /typename w_Test<Z>::type*/
?
And I still don't know why the dispatch of
static false_value func( Z &, base & );
would be taken as you expect, any reference?
I'm fighting myself as I did wrote the code to do this thing with MSVC8,
even strange it works.
public:
static const bool value =
sizeof(func(make_test(), make_derived())) == sizeof(true_value);
};
// some tests
// is_defined_test will fail if T is not defined
template <typename T> class is_defined_test
{
typedef char (&type)[ sizeof( T ) ];
};
// has_member_orange will fail if T had no orange
template <typename T> class has_member_orange
{
static T& make_T();
public:
typedef char (&type)[ sizeof( make_T().orange ) ];
};
// test code
#include <iostream>
#include <ostream>
struct A {};
struct B;
struct C { char orange; };
int main()
{
std::cout << sfinae_test< has_member_orange,C>::value << '\n';
std::cout << sfinae_test< has_member_orange,A>::value << '\n';
// comment the line
std::cout << sfinae_test< is_defined_test,B>::value << '\n';
// comment the line
std::cout << sfinae_test< is_defined_test,A>::value << '\n';
}
------------------------------------------------------------------------
so the code is supposed to work after comment the two lines, right?
anyway, the code does not compile with MSVC8.
compiles with icc9.1, but outputs
0
0
which is not expected.
here it means nothing than two test cases, I'd say, they are arguing
something.
--
Thanks
Barry