Boost logo

Boost Users :

Subject: Re: [Boost-users] Constraining unordered template constructors
From: Edward Diener (eldiener_at_[hidden])
Date: 2013-05-12 03:45:56


On 5/12/2013 12:37 AM, Michael Marcin wrote:
> I have a class that I want to have accept 0-5 arguments in any order for
> it's constructor. Each argument is a const references to one of 5
> possible class types. All arguments must be distinct in type, passing
> the same type twice in an error. Passing an argument that is not one of
> the 5 supported types is also an error.
>
> How do you go about implementing something like this?
>
> I figure first you have to create a constructor for each arity but then
> how do you constrain the constructors as described above? I imagine it
> would involve enable_if.
>
> class widget
> {
> widget();
>
> template< class A >
> widget( const A& a );
>
> template< class A, class B >
> widget( const A& a, const B& b );
>
> template< class A, class B, class C >
> widget( const A& a, const B& b, const C& c );
>
> template< class A, class B, class C, class D >
> widget( const A& a, const B& b, const C& c, const D& d );
>
> template< class A, class B, class C, class D, class E >
> widget( const A& a, const B& b, const C& c, const D& d, const E& e );
> };

Look at variadic templates if your compiler supports it, and look at
Boost variant for specifying a set of possible types for a given parameter.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net