Boost logo

Boost Users :

Subject: Re: [Boost-users] [enable_if] How to enable a constructor
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2011-03-29 07:12:10


On Tue, Mar 29, 2011 at 11:51 AM, Michael Kaes <michael.kaes_at_[hidden]>wrote:

> Hi all,
>
> I am playing around with enable_if and I want to create a constructor
> switch.
> The code is the following:
>
> ------------------------------------------------
> struct NullType{};
> struct TestType{};
> struct NonNull{};
>
> template<typename T, typename U = NullType>
> struct TemplateStruct
> {
> TemplateStruct(int i, typename boost::enable_if<boost::is_same<U,
> NullType>, void* >::type dummy = 0)
> {
> std::cout << "One Param == " << i << std::endl;
> }
>
> TemplateStruct(int i, int j, typename
> boost::disable_if<boost::is_same<U, NullType>, void* >::type dummy =
> 0)
> {
> std::cout << "Two Param == " << i << "," << j << std::endl;
> }
> };
>
> int main(int /*argc*/, char**)
> {
> TemplateStruct<TestType>(1);
> TemplateStruct<TestType,NonNull>(1,2);
> return 0;
> }
> -------------------------------------
>
> I want that the first Ctor is only available when a NullType is passed
> in and if not that the second one is available but not the first.
> I don't want to do a class specialization because the class is rather
> big and extracting all the methods to a common class would be
> difficult.
> So how can I reuse the template parameter U for the Ctors enable_if?
> Or is this simply not possible?
>
> Regards,
> Michael
>

Hi!

Here is the answer to your question. I had a similar problem.

http://lists.boost.org/boost-users/2007/11/31849.php

<http://lists.boost.org/boost-users/2007/11/31849.php>With Kind Regards,
Ovanes



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