2009/4/25 Siegfried Kettlitz <siegfried.kettlitz@googlemail.com>
Hello,

what i like to do, is to detect the presence of a particular type of
constructor for a class in a template. It is possible to use template
overloading to detect the presence of members or member functions
(like with::some_func(int) ), but since it is not possible(?) to take
a pointer to a constructor ( like with::with(int) ), this method does
not work to detect the present types of the templated class T.

Consider this pseudocode:
struct with {
 with( int );
}

struct without {
 without( void );
}

template<typename T>
T* do_construct() {
 a) return new T(int) if T::T(int) present;
 b) return new T() if T::T(int) not present;
}

int main( void ) {
 do_construct<with>();
 do_construct<without>();
}


So far i did not find anything useful in type_traits to make the
do_construct-function work without modifying the classes (adding
inheritance or some static member). Does anyone know another
possibility to do this, or is it not possible using the current
standard of c++?

It's possible, because constructor of 'with' is not explicit.

#include <boost/type_traits/is_convertible.hpp>

struct with {
  with(int) {}
};

struct without {
  without() {}
};

template <class T>
T* construct(boost::mpl::true_) {
  return new T(42);
}

template <class T>
T* construct(boost::mpl::false_) {
  return new T();
}

template<typename T>
T* do_construct() {
  return construct<T>(boost::is_convertible<int, T>());
}

int main() {
 do_construct<with>();
 do_construct<without>();
}

I don't know how to solve this problem if constructor was explicit.

Roman Perepelitsa.