Re: Is this valid code?
Nicola Musatti <nicola.musatti@gmail.com> wrote:
Hallo,
The following program is supposed to use SFINAE to identify whether a
specific template specialization is defined:
#include <iostream>
#include <ostream>
class A {};
class B {};
template <typename T> class X;
template <> class X<A> {};
template <template <typename> class C, typename T> class is_defined {
typedef char no;
typedef char (&yes)[2];
why the reference? is size although probably > 1 is dependent on the
compiler while sizeof(char[2]) == 2.
typedef char[2] yes;
looks better.
static no fn( ... );
static yes fn(C<T>);
probably should be:
static yes fn(typename C<T> );
static C<T> const& make();
get rid of the reference make is not really called and then
C<T> must be a complete type.
static C<T> make();
public:
static const bool value = sizeof( fn( make() ) ) == sizeof(yes);
};
template <template <class>class C,class T>
struct is_defined
{
struct yes {char x[2];};
static char foo(...);
template <class U>
static yes foo(C<U>);
static C<T> make();
public:
const static bool value = sizeof(foo(make())) != 1;
};
looks like it should work. If when this is invoked C<T> is an
incomplete or no existant type then ellisis function is only one
on the valid list and value == false; If C<T> is a complete type
then its on the list for the templated static function and value ==
true;
Not tested.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]