Boost logo

Boost Users :

Subject: Re: [Boost-users] Problem using enable_if with gcc 3.4.4
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2009-12-03 16:38:50


On Thu, 3 Dec 2009, gao_bolin_at_[hidden] wrote:

> Hi,
>
> I am having some trouble using enable_if with gcc 3.4.4 (code below). Basically it is working as expected with functions ("bar"), but using it on member functions of a class ("Foo"), I am getting the error that this member function cannot be overloaded.
>
> Visual 2008 and gcc4 seem to be ok with it though.
>
> Any explanation and work around appreciated.
>
> B.
>
>
> --
>
> #include <iostream>
>
> #include <boost/type_traits.hpp>
> #include <boost/utility/enable_if.hpp>
>
> // This won't compile with gcc 3.4.4
> class Foo
> {
> public:
> template < typename T >
> typename boost::enable_if<boost::is_same<T, int>, T>::type
> bar(int x)
> {
> std::cout << "A" << std::endl;
> return x;
> }
>
> template < typename T >
> typename boost::disable_if<boost::is_same<T, int>, T>::type
> bar(int x)
> {
> std::cout << "B" << std::endl;
> return x;
> }
> };

Try making the enable_if a parameter of the member function:

class Foo
{
public:
  template < typename T >
  int bar(
    int x,
    typename boost::enable_if<boost::is_same<T, int>, T>::type* = 0)
  {
    std::cout << "A" << std::endl;
    return x;
  }

  // and the same for the second version.
};

There is more information on these workarounds in section 3.4 of
<URL:http://www.boost.org/doc/libs/1_41_0/libs/utility/enable_if.html>.

-- Jeremiah Willcock


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