How to detect the presence of a particular type of constructor?
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++? Regards, Siegfried Kettlitz
2009/4/25 Siegfried Kettlitz
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
Thanks, that is just what i was looking for. :) Apparently that's the key point for detecting the implicit conversion constructor: template< typename From > struct does_conversion_exist { template< typename To > struct result_ { static no_type BOOST_TT_DECL _m_check(...); static yes_type BOOST_TT_DECL _m_check(To); static From _m_from; enum { value = sizeof( _m_check(_m_from) ) == sizeof(yes_type) }; }; }; Can this be extended to constructors with more than one parameter? As far as i can see, it applys only to conversion constructors.
AMDG Siegfried Kettlitz wrote:
Thanks, that is just what i was looking for. :)
Apparently that's the key point for detecting the implicit conversion constructor: template< typename From > struct does_conversion_exist { template< typename To > struct result_ { static no_type BOOST_TT_DECL _m_check(...); static yes_type BOOST_TT_DECL _m_check(To); static From _m_from; enum { value = sizeof( _m_check(_m_from) ) == sizeof(yes_type) }; }; };
Can this be extended to constructors with more than one parameter?
There is no known way to detect constructors except for the special case of implicit conversions.
As far as i can see, it applys only to conversion constructors.
Right. In Christ, Steven Watanabe
2009/4/25 Steven Watanabe
AMDG
Siegfried Kettlitz wrote:
Thanks, that is just what i was looking for. :)
Apparently that's the key point for detecting the implicit conversion constructor: template< typename From > struct does_conversion_exist { template< typename To > struct result_ { static no_type BOOST_TT_DECL _m_check(...); static yes_type BOOST_TT_DECL _m_check(To); static From _m_from; enum { value = sizeof( _m_check(_m_from) ) == sizeof(yes_type) }; }; };
Can this be extended to constructors with more than one parameter?
There is no known way to detect constructors except for the special case of implicit conversions.
See also the thread ( http://groups.google.com/group/boost-list/browse_thread/thread/e15440218b851... ) where I asked for exactly the same thing. Roman Perepelitsa.
participants (3)
-
Roman Perepelitsa
-
Siegfried Kettlitz
-
Steven Watanabe