[MPL] wrapper for if_ selecting integral_c

Hi, I would like to suggest an addition to Boost MPL. In the project I am working on I often need to select integral values, either using MPL or home-brew template logic. Where MPL is preferred due to consistency. This requires me to do something like: int value = if_<condition, int_<value1>, int_<value2> >::type::value; Which (when adding namespaces to the above) is too much typing in my opinion and reduces readability of my code. Would it be useful to add a new variant of if_ to Boost MPL that would enable the following? (by combining the syntax of if_ and integral_c) type value = if_integral_c<condition, type, value1, value2>::value Best regards, Roel Jordans

on Mon Oct 20 2008, "Jordans, R." <r.jordans-AT-student.tue.nl> wrote:
Hi,
I would like to suggest an addition to Boost MPL. In the project I am working on I often need to select integral values, either using MPL or home-brew template logic. Where MPL is preferred due to consistency.
This requires me to do something like:
int value = if_<condition, int_<value1>, int_<value2> >::type::value;
Which (when adding namespaces to the above) is too much typing in my opinion and reduces readability of my code.
Would it be useful to add a new variant of if_ to Boost MPL that would enable the following? (by combining the syntax of if_ and integral_c)
type value = if_integral_c<condition, type, value1, value2>::value
You can easily write that template yourself if you need it. What is the "type" argument above supposed to do? -- Dave Abrahams BoostPro Computing http://www.boostpro.com

type value = if_integral_c<condition, type, value1, value2>::value
You can easily write that template yourself if you need it. What is the "type" argument above supposed to do?
The idea there would be the same as in integral_c, to specify the type of value to select I've written the template myself, but I need to be able to compile it on different systems (Linux, OS-X and Windows) and I'm not sure if every compiler will like it My code for the template is as follows: namespace boost { namespace mpl { template <typename C, typename T, T value1, T value2> struct if_integral_c { static const T value = if_<C, integral_c<T, value1>, integral_c<T, value2> >::type::value; }; } } But from what I've understood not all compilers like it when you use a template typename parameter as type for other template parameters Is there a more universal solution to this or should I fix the types of value1 and value2 and cast it later to the correct type? Roel

on Tue Oct 21 2008, "Jordans, R." <r.jordans-AT-student.tue.nl> wrote:
I've written the template myself, but I need to be able to compile it on different systems (Linux, OS-X and Windows) and I'm not sure if every compiler will like it
My code for the template is as follows:
namespace boost { namespace mpl { template <typename C, typename T, T value1, T value2> struct if_integral_c { static const T value = if_<C, integral_c<T, value1>, integral_c<T, value2> >::type::value; }; } }
template <typename C, typename T, T value1, T value2> struct if_integral_c : integral_c<T,(C::type::value?value1:value2)> {}; would be more correct and more interoperable.
But from what I've understood not all compilers like it when you use a template typename parameter as type for other template parameters
Sorry, I don't understand what you mean. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

template <typename C, typename T, T value1, T value2> struct if_integral_c : integral_c<T,(C::type::value?value1:value2)> {};
would be more correct and more interoperable.
Ok, I will use that, thanks for the suggestion
But from what I've understood not all compilers like it when you use a template typename parameter as type for other template parameters
Sorry, I don't understand what you mean.
I'm not sure if all compilers understand templates in the form: template<typename T, T foo> Or is this only for obscure compilers? Roel

on Tue Oct 21 2008, "Jordans, R." <r.jordans-AT-student.tue.nl> wrote:
I'm not sure if all compilers understand templates in the form:
template<typename T, T foo>
Or is this only for obscure compilers?
I don't know of any where that fails. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (4)
-
Christian Holmquist
-
David Abrahams
-
Jordans, R.
-
Steven Watanabe