[mpl] Using at_c within find_if
Hi there, I'm trying to find a solution for looking for type vector containing a specific type. On another thread I was suggested to use at_c within the find_if algorithm. Unfortunately the code doesn't compile. #include "boost/mpl/vector.hpp" #include <boost/mpl/find_if.hpp> #include <boost/mpl/at.hpp> using namespace std; using namespace boost; typedef mpl::vector<char, char> vec_1; typedef mpl::vector<char, int> vec_2; typedef mpl::vector<vec_1, vec_2> mat; typedef mpl::find_if<mat, is_same<mpl::at_c<mpl::_1,1>, int> >::type iter; int _tmain(int argc, _TCHAR* argv[]) { BOOST_STATIC_ASSERT(( is_same< mpl::deref<iter>::type, vec_2 >::value )); return 0; } Might this be a bug in the MPL lib? Thanks for any help, Christian
Christian Henning wrote:
Hi there, I'm trying to find a solution for looking for type vector containing a specific type. On another thread I was suggested to use at_c within the find_if algorithm. Unfortunately the code doesn't compile.
Placeholder substitution won't work in templates taking non-type arguments, such as at_c.
typedef mpl::find_if<mat, is_same<mpl::at_c<mpl::_1,1>, int> >::type iter;
Try using 'at' and 'int_': typedef find_if<mat, is_same<at<_1,int_<1> >, int> >::type iter; Regards, Tobias
Thanks a lot for your hint. It works perfectly. Here is the code: #include "boost/mpl/vector.hpp" #include <boost/mpl/find_if.hpp> #include <boost/mpl/at.hpp> using namespace std; using namespace boost; typedef mpl::vector<char, char> vec_1; typedef mpl::vector<char, int> vec_2; typedef mpl::vector<double, int> vec_3; typedef mpl::vector<vec_1, vec_2, vec_3> mat; typedef mpl::find_if< mat, is_same< mpl::at< mpl::_1, mpl::int_<0> >, double> >::type iter; int _tmain(int argc, _TCHAR* argv[]) { BOOST_STATIC_ASSERT(( is_same< mpl::deref<iter>::type, vec_3 >::value )); return 0; }
On 3/28/07, Tobias Schwinger <tschwinger@isonews2.com> wrote:
Christian Henning wrote:
Hi there, I'm trying to find a solution for looking for type vector containing a specific type. On another thread I was suggested to use at_c within the find_if algorithm. Unfortunately the code doesn't compile.
Placeholder substitution won't work in templates taking non-type arguments, such as at_c.
typedef mpl::find_if<mat, is_same<mpl::at_c<mpl::_1,1>, int> >::type iter;
Try using 'at' and 'int_':
typedef find_if<mat, is_same<at<_1,int_<1> >, int> >::type iter;
Regards, Tobias
I am curious why at_c doesn't do the same thing. Shouldn't these be equivalent. Chris
Chris Weed wrote:
On 3/28/07, Tobias Schwinger <tschwinger@isonews2.com> wrote:
Christian Henning wrote:
Hi there, I'm trying to find a solution for looking for type vector containing a specific type. On another thread I was suggested to use at_c within the find_if algorithm. Unfortunately the code doesn't compile. Placeholder substitution won't work in templates taking non-type arguments, such as at_c.
typedef mpl::find_if<mat, is_same<mpl::at_c<mpl::_1,1>, int> >::type iter; Try using 'at' and 'int_':
typedef find_if<mat, is_same<at<_1,int_<1> >, int> >::type iter;
Regards, Tobias
I am curious why at_c doesn't do the same thing. Shouldn't these be equivalent.
Yes, ideally. But it would be rather heavy, implementation-wise. You can decompose a type created by specializing a template like this: template<typename T> struct template_traits; template<template <typename> class Tpl, typename T0> struct template_traits< Tpl<T0> > { static const int arity = 1; typedef T0 first_type; }; template<template<typename,typename> class Tpl, typename T0, typename T1> struct template_traits< Tpl<T0,T1> > { static const int arity = 2; typedef T0 first_type; typedef T1 second_type; }; //... That's roughly what the MPL lambda facility does. Now imagine you want to support int typed (non-type) template parameters. Right, we'd need all combinations - N^2 specializations - only for ints. Also note that several compilers won't handle the code above, so MPL needs to emulate it. Regards, Tobias
participants (3)
-
Chris Weed
-
Christian Henning
-
Tobias Schwinger