[enable_if] Compile Time Member Overloading

Hi there,
can someone tell me why the following doesn't compile with VS2010 SP1?
#include

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
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

#include
#include #include using namespace boost;
struct foo { void bla() { read_data_24
(); } 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