Boost logo

Boost :

Subject: Re: [boost] detecting a constructor with a specific signature
From: Daniel Frey (d.frey_at_[hidden])
Date: 2010-01-27 01:51:06


On 26.01.2010, at 23:36, Kenny Riddile wrote:

> I'm trying to create a type trait to detect the presence of a constructor with a very specific signature.

You might want to have a look into is_constructible. It's a new type trait from the upcoming standard <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3000.pdf>, and a partial implementation is possible. Here's a version for GCC 4.4+ (requires -std=c++0x):

#include <type_traits>

namespace boost
{
  namespace is_constructible_impl
  {
    template< typename T >
    typename std::add_rvalue_reference< T >::type declval();

    template< typename T >
    decltype( T(), std::true_type() ) select( int );

    template< typename T, typename A >
    decltype( static_cast< T >( declval< A >() ), std::true_type() ) select( int );

    template< typename T, typename A1, typename A2 >
    decltype( T( declval< A1 >(), declval< A2 >() ), std::true_type() ) select( int );

    template< typename T, typename A1, typename A2, typename A3 >
    decltype( T( declval< A1 >(), declval< A2 >(), declval< A3 >() ), std::true_type() ) select( int );

    template< typename T, typename A1, typename A2, typename A3, typename A4 >
    decltype( T( declval< A1 >(), declval< A2 >(), declval< A3 >(), declval< A4 >() ), std::true_type() ) select( int );

    // A variadic version of the above is not possible,
    // GCC 4.4 crashes with an internal compiler error.
    // GCC 4.5 will likely provide is_constructible itself

    template< typename, typename... >
    std::false_type select( ... );
  }

  template< typename T, typename... As >
  struct is_constructible
    : std::is_same< decltype( is_constructible_impl::select< T, As... >( 0 ) ), std::true_type >
  {
  };
}

HTH.

Regards, Daniel




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk