
Eduardo Bezerra
I'm reading the C++ Template Metaprogramming book and I'm not understanding the following code from page 65-66:
template <class T> struct param_type : mpl::eval_if < typename boost::is_scalar<T>::type , mpl::indentity<T> , boost::add_reference<T const>
{};
template <class T> struct param_type : mpl::eval_if < boost::is_scalar<T> --> ^^^^^^^^^^^^^^^^^^^^^^^ <-- , mpl::indentity<T> , boost::add_reference<T const>
{};
After reading again and again the explanation I still don't get why we don't need to use typename ....::type anymore. The text explains that all Boost's integral metafunctions suply a nested ::value, but how it is used in this context ?
Well, typename boost::is_scalar<T>::type is some mpl::bool_<X> which happens to be a class that fulfils the requirements that eval_if places on its first argument. http://www.boost.org/libs/mpl/doc/refmanual/eval-if.html shows that the requirement is that the argument be a model of Integral Constant. If you click through to the requirements page http://www.boost.org/libs/mpl/doc/refmanual/integral-constant.html for Integral Constant you can see what those are; (**) they include the requirement that the class have a nested ::value. However, we can drop typename...::type because boost::is_scalar<T> also happens to be a model of Integral constant with the same properties as its nested ::type. That might be achieved this way: template <class T> struct is_scalar_impl { typedef mpl::bool_< ... > type; }; template <class T> struct is_scalar : is_scalar_impl<T>::type // derive from mpl::bool_< ... > { typedef typename is_scalar_impl<T>::type type; }; It turns out that any class derived from mpl::bool_<X> is also a model of Integral Constant with the same properties as mpl::bool_<X> has. I hope this explains it. (**) Aleksey, I think Integral Constant is much too strong a requirement for the first argument to eval_if. Most of those things requiring a bool_ or similar really just need a nested ::value. -- Dave Abrahams Boost Consulting www.boost-consulting.com