Boost logo

Boost Users :

From: Daniel Krügler (dsp_at_[hidden])
Date: 2008-05-06 08:12:28


Germán Diago Gómez wrote:
> Hello. I'm trying to compile this code, but it doesn't do the trick and
> I don't know why.

Without looking into further details, the following is a
misuse of enable_if:

> template <class Tuple, class UnaryF, int i = 0>
> struct tuple_for_each_helper
> {
> private:
> Tuple & t_;
> UnaryF f_;
> public:
> typedef typename UnaryF::result_type result_type;
>
> tuple_for_each_helper(Tuple & t, UnaryF f) : t_(t), f_(f)
> {
> }
>
>
> result_type operator()(typename
> boost::enable_if<is_same<result_of<UnaryF (const Tuple &)>, void>
>>::type * = 0)

Here is the error: Your are trying to use enable_if/SFINAE in an
"unprotected" context. What does that mean?

SFINAE causes a *silent* failure during an *attempt* to instantiate
a template. The only template here is tuple_for_each_helper which
was already instantiated, before the actual failure could take
place.

a) If you want to SFINAE-shield operator(), you have to define
that operator as a function template.

b) If you want to prevent to SFINAE-shield the complete
tuple_for_each_helper class template, you have to add one
further "SFINAE" template parameter to the class template and
you have to add the SFINAE expression there.

It's rather easy, if you medidate about this for a while:
Once an class template *is* instantiated, all member *declarations*
(this is the short rule) must be valid. This is obviously not
the case for the member operator(), *if* enable_if does not
have a member type.

Greetings from Bremen,

Daniel Krügler


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