Boost logo

Boost Users :

Subject: Re: [Boost-users] [Enable-if] Selectively instantiating methods.
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2011-09-23 22:15:52


On Fri, 23 Sep 2011 00:51:40 -0700, Robert Jones <robertgbjones_at_[hidden]>
wrote:

> Hi All
>
> This doesn't work.
>
> template<typename T> struct S
> {
> typename disable_if<is_const<T>, int>::type mutating_method();
> };
>
> I understand why not, although only after the compiler threw it out, but
> obviously (in hindsight),
> the method itself is not a template, so can't be selectively
> instantiated.
>
> The question is whether and how I can achieve the effect I'm after, ie.,
> that the method
> is not present if the struct is instantiated with a const type?
>
> Thx, Rob.

See http://thread.gmane.org/gmane.comp.lib.boost.devel/222181/focus=222402

Short answer (from the cited post):

>> template <class U>
>> struct T
>> {
>> void f()
>> {
>> f_impl(<static_cast<void *>(0));
>> }
>>
>> private:
>> template <class T>
>> typename enable_if<
>> mpl::and_<
>> is_convertible<T, void *>,
>> some_condition_on<U>
>> >::type
>> f_impl(T)
>> { .... }
>> };

Basically introduce a dummy parameter with a dummy template type that's
not used in f_impl.

Mostafa


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