From: Oz on
Hi all,
I was wondering if someone could help me with the program below. I'm
trying to figure out if there is a way to determine if a particular
class has a default constructor, copy constructor and destructor. I
know how to use the SFINAE trick to determine if a class supports a
particular operator or not (see has_plus below).

I'm having a hard time figuring out if it is even possible to use a
similar trick to detect the presence of constructors or destructors.
Ideally, I'd like a template called has_default_ctor<A>::value that
yields true or false depending on if A has a default constructor or
not. It doesn't seem to be the same as determining whether a class
has a particular member function or not since it is not possible to
get member function pointers to constructors or destructors.

Any one know whether this is even possible and if so, how might I go
about implementing it?

Thanks,
Oz

///////////////////////////////////////////////////////////////////////
#include <iostream>

using std::cout;
using std::endl;

struct bad_tag {};

struct any {
template <typename C> any(C);
};

typedef char yes;
typedef long no;

no check(bad_tag);
yes check(...);

///////////////////////////

bad_tag operator+(const any&, const any&);
template <typename A>
struct has_plus
{
static A makeA();
static const bool value = sizeof(check(makeA() + makeA())) == sizeof
(yes);
};

///////////////////////////

template <typename A>
struct has_default_ctor
{
// this won't compile if A doesn't have a default ctor
static const bool value = sizeof(check(A())) == sizeof(yes);
};

///////////////////////////

class A {
public:
A& operator+(const A&);
};

class B {
public:
B(int);
};

int main()
{
cout << "has plus A = " << has_plus<A>::value << endl;
cout << "has plus B = " << has_plus<B>::value << endl;

// should print 1
cout << "has default ctor A = " << has_default_ctor<A>::value <<
endl;

// uncomment this line and the program won't compile
// should print 0
// cout << "has default ctor B = " << has_default_ctor<B>::value <<
endl;

return 0;
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bart van Ingen Schenau on
On Feb 2, 9:15 pm, Oz <ozrictentac...(a)gmail.com> wrote:
> Hi all,
> I was wondering if someone could help me with the program below. I'm
> trying to figure out if there is a way to determine if a particular
> class has a default constructor, copy constructor and destructor. I
> know how to use the SFINAE trick to determine if a class supports a
> particular operator or not (see has_plus below).
>
> I'm having a hard time figuring out if it is even possible to use a
> similar trick to detect the presence of constructors or destructors.
> Ideally, I'd like a template called has_default_ctor<A>::value that
> yields true or false depending on if A has a default constructor or
> not. It doesn't seem to be the same as determining whether a class
> has a particular member function or not since it is not possible to
> get member function pointers to constructors or destructors.
>
> Any one know whether this is even possible and if so, how might I go
> about implementing it?

I think it is implementable, because the Boost::Type_Traits library
has a few similar traits. You might want to take a look at their
implementation.

>
> Thanks,
> Oz
>

Bart v Ingen Schenau


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]