Boost logo

Boost :

Subject: Re: [boost] [C++0x] Report from Frankfurt committee meeting
From: Andrew Sutton (andrew.n.sutton_at_[hidden])
Date: 2009-08-17 10:45:33


>
>
>> Function template argument defaults are also very helpful, since
>>> enable_if can be applied to the template argument rather than a
>>> function argument or return value. That results in cleaner,
>>> easier-to-read, code.
>>>
>>> Sorry for the off-topicness but, are they any sample use-case for
>> enable_if in template argument somewhere to look at ?
>>
>
I've actually been experimenting with this for the last couple of days, and
have been having some problems overloading with enable_if in template
parameters. The technique works fine if there is only function definition.
Given multiple overloads, the compiler (for me, GCC 4.5) will give
redefinition errors.

template <
    typename T,
    typename = typename enable_if<std::is_integral<T>>::type>
void foo(T const&) { cout << "1\n"; }

template <
    typename T,
    typename = typename disable_if<std::is_integral<T>>::type>
void foo(T const&) { cout << "2\n"; }

It doesn't work if you try to push some part of the enable_if into the
function declaration either. Fopr example, if you use the form:

template <
    typename T,
    typename Enabler = typename enable_if<std::is_integral<T>>::type>
Enabler foo(T const&)

Still a redefinition. What about?

template <
    typename T,
    typename Enabler = enable_if<std::is_integral<T>>>
typename Enabler::type foo(T const&)

Still a redefinition.

It doesn't really seem to matter how you try to write or force the enable_if
to work, the compiler seems to want to call these the same function - which,
I suppose they are until you try to instantiate them. The only thing that
does (still) work is to write the enabler as a return type or function
parameter. It may also be the case that this is supposed to work, but hasn't
been implemented by GCC. I'm not certain.

Andrew Sutton
andrew.n.sutton_at_[hidden]


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