[enable_if] Compile Time Member Overloading
Hi there, can someone tell me why the following doesn't compile with VS2010 SP1? #include <boost/mpl/and.hpp> #include <boost/type_traits/is_same.hpp> #include <boost/utility/enable_if.hpp> using namespace boost; struct foo { void bla() { read_data_24<int, int>(); } template< typename T1 , typename T2 > void read_data_24( typename enable_if< mpl::and_< mpl::true_ , mpl::bool_< is_same< T1, T2 >::type::value > > >::type* /* ptr */ = 0 ) { int i = 9; } }; int main(int argc, char* argv[]) { return 0; } I get the error: 1>gil_test_vc10.cpp(25): error C2039: 'type' : is not a member of 'boost::enable_if<Cond>' 1> with 1> [ 1> Cond=boost::mpl::and_<boost::mpl::true_,boost::mpl::bool_<false>> 1> ] I tried a couple of hours to have make is happen but I don't get it. Thanks, Christian
What happens if you drop the ::type from is_same? void read_data_24( typename enable_if< mpl::and_< mpl::true_, mpl::bool_< is_same< T1, T2 >*::value* > > >::type* /* ptr */ = 0) (I don't have VS compiler so can't test - but dropping ::type from is_same still compiles fine on gcc 4.7.0 for me)
Hi Steve, On Sun, Nov 18, 2012 at 7:27 PM, Steve Lorimer <steve.lorimer@gmail.com> wrote:
What happens if you drop the ::type from is_same?
void read_data_24( typename enable_if< mpl::and_< mpl::true_, mpl::bool_< is_same< T1, T2 >::value > > >::type* /* ptr */ = 0)
(I don't have VS compiler so can't test - but dropping ::type from is_same still compiles fine on gcc 4.7.0 for me)
Yes that worked for me. Thanks a lot! One more thing do I actually need to build a mpl::bool_ out of the is_same function? Christian
Christian Henning wrote:
One more thing do I actually need to build a mpl::bool_ out of the is_same function?
enable_if requres an integral constant wrapper. There is a variant called enable_if_c which allows you to skip that in this case (automatically wraps it, I believe, like mpl::vector_c): #include <boost/mpl/and.hpp> #include <boost/type_traits/is_same.hpp> #include <boost/utility/enable_if.hpp> using namespace boost; struct foo { void bla() { read_data_24<int, int>(); } template< typename T1, typename T2> void read_data_24(typename enable_if_c< is_same< T1, T2 >::value>::type* = 0) { int i = 9; } }; int main(int argc, char* argv[]) { return 0; } HTH, Nate
#include <boost/mpl/and.hpp> #include <boost/type_traits/is_same.hpp> #include <boost/utility/enable_if.hpp>
using namespace boost;
struct foo { void bla() { read_data_24<int, int>(); }
template< typename T1, typename T2> void read_data_24(typename enable_if_c< is_same< T1, T2 >::value>::type* = 0)
{ int i = 9; } };
int main(int argc, char* argv[]) { return 0; }
Thanks for info. It's a good idea to use enable_if_c! Christian
Hi Christian
One more thing do I actually need to build a mpl::bool_ out of the is_same function?
No, in fact you can drop the bool, and and true; and just use enable_if. enable_if will even use the boolean operator to get to the underlying boolean ::value so you drop even more syntax: template< typename T1 , typename T2 > void read_data_24( typename enable_if< is_same< T1, T2 > > ::type* /* ptr */ = 0) { int i = 9; } HTH Steve
No, in fact you can drop the bool, and and true; and just use enable_if. enable_if will even use the boolean operator to get to the underlying boolean ::value so you drop even more syntax:
template< typename T1 , typename T2 > void read_data_24( typename enable_if< is_same< T1, T2 > > ::type* /* ptr */ = 0) { int i = 9;
}
Thanks, that works, as well! Christian
participants (3)
-
Christian Henning
-
Nathan Crookston
-
Steve Lorimer