Question

I am trying to to do a static assertion to check that for two types A and B; class A should have a public constructor of signature A(B& b) or A(const B& b).

I would like to have something of sort of is_constructable_from<A, B>::value which evaluates to true if there exists a public A(B& b) or A(const B& b) or A(B b).

I guess this is somewhat different than boost is_convertible type_trait.

How do I achieve this?

Have I missed something which will do this in Boost Type Traits or Concept Check library?

Right now I need this to do a static check that the expression A a(b) is a valid one, which as suggested by Sehe is a less restrictive than the previous concept. A solution for both the cases are welcome. I am allowed to use Boost.

Was it helpful?

Solution

Constructors are not member functions - they don’t have an address that you can take, so it is impossible to test for their presence in a straightforward way with SFINAE*.

While it isn't exactly what you asked for, with C++11 you can get close with std::is_constructible, and with C++03 you have to rollout your own:

template<class A, class B = void>
struct is_constructible;

template<class A, class B>
struct is_constructible { 

    template<class U> static U declval();    
    template<std::size_t N> struct dummy;
    template<class U> static char test(dummy<sizeof(U(declval<B>()))>*);        
    template<class U> static long test(...);

    static const bool value = sizeof(test<A>(0)) == sizeof(char); 
};

* The following is an interesting attempt, but to the best of my understanding it doesn't work well across compilers:
Check at compile time class constructor signature

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top