C++ Template Metaprogramming Book
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 ? Any help would be greatly appreciated. Best regards, Eduardo
On 8/4/05, 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 ?
The eval_if is a lazy metafunction, so that mpl::identity nor boost::add_reference is evaluated before boost::is_scalar and chosed which to evaluate. That way even when one of the metafunctions results in a compilation error, only the right one will be evaluated and so wont happen in any compilation error(and will be faster if you have two expensive metafunctions either). And as boost::is_scalar has ::value either, then it wont be needed to use typename xxx<T>::type, that would be used as typename xxx<T>::type::value inside eval_if, only typename xxx<T>::value will be used for eval_if, begin evaluted lazily either. I think it is this... If I'm wrong, or I have missed the question, somebody correct me, please :P.
Any help would be greatly appreciated.
Best regards, Eduardo
-- Felipe Magno de Almeida Developer from synergy and Computer Science student from State University of Campinas(UNICAMP). Unicamp: http://www.ic.unicamp.br Synergy: http://www.synergy.com.br "There is no dark side of the moon really. Matter of fact it's all dark."
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
participants (3)
-
David Abrahams
-
Eduardo Bezerra
-
Felipe Magno de Almeida