Victor said:
Victor Bazarov wrote:
template<class T, class U> struct same { enum { yes = 0 }; };
template<class T> struct same<T,T> { enum { yes = 1 }; };
[...]
Can use boost::is_same to save writing above and ...
[...]
Why use a third-party library when it's possible without it?
Simple. It follows a standard convention. Change name of yes member to
value and your version would work with enable_if, in version above,
however though it then works in that case, in the following example I
have modified the code so that I am selecting on condition that both T
and U are the same and both are pointers. Feel free to try
uncommenting the handrolled version with changed name and using that
instead. At this point its rather more work to keep redoing all the
metafunctions. Note also that both is_same and is_pointer are scheduled
to be included in the standard library in future. Use the standard
functions because they follow standard practise and everyone knows what
their exact layout syntax and purpose is. They've been tested and
debugged and will be more portable. IOW it saves time and effort all
round.
I'm sure you knew all that though. Just accept that my way is better
for once ;-)
cheers
Andy Little
------------------
#include <iostream>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/mpl/and.hpp>
using namespace std;
/////////////////////////////
// hint: use value member so will work with enable_if
template<class T, class U> struct same { enum { value = 0 }; };
template<class T> struct same<T,T> { enum { value = 1 }; };
/////////////////////////////
template<class T, class U>
typename boost::enable_if<
boost::mpl::and_<
boost::is_same<T,U>,
// same said:
foo()
{
cout << "foo<T*,T*>\n";
}
template<class T, class U>
typename boost::disable_if<
boost::mpl::and_<
boost::is_same<T,U>,
// same said:
foo()
{
cout << "foo<T,U>\n";
}
int main(int argc,char *argv[])
{
foo<int,double>();
foo<double,int>();
foo<char,char>();
foo<const char*,const char*>();
}