enable_if for class members

In a class, I want to have one member function which has different
implementations depending on one of the template parameters. I tried
to use enable_if
template<bool b>
struct foo
{
typename boost::enable_if_c::type
get_val( )
{ return 2; }
typename boost::disable_if_c::type
get_val()
{ return 3; }
};
But when I use foo in code, it doesn't work, complaning that:
error: no type named 'type' in 'struct boost::enable_if_c

AMDG Christopher Jefferson wrote:
In a class, I want to have one member function which has different implementations depending on one of the template parameters. I tried to use enable_if
template<bool b> struct foo { typename boost::enable_if_c::type get_val( ) { return 2; }
typename boost::disable_if_c::type get_val() { return 3; } };
But when I use foo in code, it doesn't work, complaning that: error: no type named 'type' in 'struct boost::enable_if_c
'
enable_if doesn't work for members of a template class unless those members are themselves templates.
Am I not using enable_if in the correct way? Is there any easy way to do waht I'm trying to do? I used to do this by declaring an inner class and overloading that, but then it is necessary to pass in the class and the code looks very messy.
You could use private member functions template<bool b> struct foo { public: int get_val() { return(get_val(boost::mpl::bool_<b>())); } private: int get_val(boost::mpl::true_) { return(2); } int get_val(boost::mpl::false_) { return(3); } }; In Christ, Steven Watanabe

On Fri, Jan 30, 2009 at 6:21 AM, Christopher Jefferson
In a class, I want to have one member function which has different implementations depending on one of the template parameters. I tried to use enable_if
template<bool b> struct foo { typename boost::enable_if_c::type get_val( ) { return 2; }
typename boost::disable_if_c::type get_val() { return 3; } };
But when I use foo in code, it doesn't work, complaning that: error: no type named 'type' in 'struct boost::enable_if_c
' Am I not using enable_if in the correct way? Is there any easy way to do waht I'm trying to do? I used to do this by declaring an inner class and overloading that, but then it is necessary to pass in the class and the code looks very messy.
You could specialize the member function: template<bool> struct foo { int get_val(); }; template <> int foo<false>::get_val() { return 2; } template <> int foo<true>::get_val() { return 3; } Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (3)
-
Christopher Jefferson
-
Emil Dotchevski
-
Steven Watanabe