Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-02-15 13:05:42


Christian Henning wrote:
> Hi there, I'm having a problem with using enable_if. For the sake of
> simplicity I have created the following test code which compiles fine.
>
> #include <string>
>
> #include <boost/mpl/and.hpp>
> #include <boost/utility/enable_if.hpp>
>
> using namespace std;
> using namespace boost;
>
> struct reader {};
>
> template< typename String > struct is_string : public mpl::false_ {};
> template<> struct is_string<std::string> : public mpl::true_ {};
>
> template< typename Device > struct is_device : public mpl::false_ {};
> template<> struct is_device<reader> : public mpl::true_ {};
>
> template< typename String>
> struct enable_if_helper
> {
> typedef typename enable_if< typename is_string< String >::type >::type type;
> };
>
> template<typename Device >
> void
> foo( typename enable_if< typename is_device<Device>::type >::type* ptr = 0 )
> {}
>
> template<typename String>
> void foo( typename enable_if< typename is_string<String>::type
>
>> ::type* ptr = 0 )
>>
> { foo<reader>(); }
>
>
> int main(int argc, _TCHAR* argv[])
> {
> foo<std::string>();
> return 0;
> }
>
>
> If I changed the string foo() interface to use the enable_if_helper
> the code doesn't compile anymore. Why is that?
>
> I've changed the foo ( with the string template parameter ) to the following:
>
> template<typename String>
> void foo( typename enable_if_helper< String >::type* ptr = 0 )
> { foo<reader>(); }
>
>
> Anyone can help me out here? I'm using Visual Studio 8.
>

Basically, enable_if_helper is instantiated and fails and SFINAE doesn't
protect you. Use the following:

template< typename String>
struct enable_if_helper
  : enable_if< is_string< String > >
{
};

In Christ,
Steven Watanabe


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