Problem using enable_if with gcc 3.4.4
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; } }; // this would. 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; } int main() { bar<int>(1); bar<double>(1); Foo f; f.bar<int>(1); f.bar<double>(1); return 0; } ____________________________________________________ Michael Jackson, Susan Boyle, Black Eyed Peas ... Retrouvez leurs derniers titres sur http://musiline.voila.fr
On Thu, 3 Dec 2009, gao_bolin@voila.fr 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
participants (2)
-
gao_bolin@voila.fr
-
Jeremiah Willcock