Any way in current standard to implement this policy traits?

  • Follow


This comes out when I was trying to resolve a problem trying to
convert something like list<list<...<T> > > to vector<vector<...<T> >
>, to construct to return type from list<list<...<T> > >, I have to
use a policy traits, like this:

// Construct return type
template <typename T>
struct ReturnType
{
  template<typename C> static vector<typename C::value_type>
test(typename C::value_type const *);
  template<typename C> static int test(...);

  typedef typename
  IfThenElse<IsTarget<T>::Yes, typeof(ReturnType<T>::test<T>(0)),
T>::ResultT
    return_type;
};

IsTarget is a policy class to determine if T is a type of list.

However, to define return_type as T::value_type if and only if T is a
type of list, I can only think of using gcc's typeof extesion, with
the help of the so called "substitution-failure-is-not-an-
error" (SFINAE).

Is there solution in the current C++ standard?

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply boodweb (3) 8/30/2008 8:48:51 AM

On Aug 30, 10:48 am, bood <bood...@gmail.com> wrote:

> However, to define return_type as T::value_type if and only if T is a
> type of list, I can only think of using gcc's typeof extesion, with
> the help of the so called "substitution-failure-is-not-an-
> error" (SFINAE).
>
> Is there solution in the current C++ standard?

Maybe I misunderstand.  Have you tried this?

#include <list>

template <typename T>
struct ReturnType
{
     template <typename C>
     struct IsList
     {
	    typedef C return_type;
     };

     template <typename C>
     struct IsList <std::list <C> > //<-- substitute with your own
policy
     {
	typedef typename std::list <C> ::return_type return_type;
     };


	typedef typename IsList <T> ::return_type return_type;
};

Kevin P. Barry

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ta0kira 8/31/2008 2:58:44 AM


bood wrote:

> This comes out when I was trying to resolve a problem trying to
> convert something like list<list<...<T> > > to vector<vector<...<T> >
>>, to construct to return type from list<list<...<T> > >, I have to
> use a policy traits, like this:
> 
> // Construct return type
> template <typename T>
> struct ReturnType
> {
>   template<typename C> static vector<typename C::value_type>
> test(typename C::value_type const *);
>   template<typename C> static int test(...);
> 
>   typedef typename
>   IfThenElse<IsTarget<T>::Yes, typeof(ReturnType<T>::test<T>(0)),
> T>::ResultT
>     return_type;
> };
> 
> IsTarget is a policy class to determine if T is a type of list.
> 
> However, to define return_type as T::value_type if and only if T is a
> type of list, I can only think of using gcc's typeof extesion, with
> the help of the so called "substitution-failure-is-not-an-
> error" (SFINAE).
> 
> Is there solution in the current C++ standard?

I am not sure whether I completely understand your problem, but maybe
something along the following lines contains ideas you could use:

#include <list>
#include <vector>

template < typename T >
struct rec_list_to_vector {

   typedef T result_type;
   typedef T argument_type;

   static
   result_type convert ( argument_type const & arg ) {
     return ( arg );
   }

};

template < typename T >
struct rec_list_to_vector< std::list<T> > {

   typedef
   std::vector< typename rec_list_to_vector<T>::result_type >
   result_type;

   typedef
   std::list< typename rec_list_to_vector<T>::argument_type >
   argument_type;

   static
   result_type convert ( argument_type const & arg ) {
     result_type result;
     for ( typename argument_type::const_iterator iter = arg.begin();
           iter != arg.end(); ++iter ) {
       result.push_back( rec_list_to_vector<T>::convert( *iter ) );
     }
     return ( result );
   }

};

template < typename T >
typename rec_list_to_vector<T>::result_type
convert_list_to_vector ( T const & t ) {
   return ( rec_list_to_vector<T>::convert( t ) );
}


int main ( void ) {
   std::list< std::list< int > > ivv;
   std::vector< std::vector< int > > ill = convert_list_to_vector( ivv );
}


Best

Kai-Uwe Bux

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Kai 8/31/2008 2:59:02 AM

On Aug 31, 4:59 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> bood wrote:
> > This comes out when I was trying to resolve a problem trying to
> > convert something like list<list<...<T> > > to vector<vector<...<T> >
> >>, to construct to return type from list<list<...<T> > >, I have to
> > use a policy traits, like this:
>
> > // Construct return type
> > template <typename T>
> > struct ReturnType
> > {
> >   template<typename C> static vector<typename C::value_type>
> > test(typename C::value_type const *);
> >   template<typename C> static int test(...);
>
> >   typedef typename
> >   IfThenElse<IsTarget<T>::Yes, typeof(ReturnType<T>::test<T>(0)),
> > T>::ResultT
> >     return_type;
> > };
>
> > IsTarget is a policy class to determine if T is a type of list.
>
> > However, to define return_type as T::value_type if and only if T is a
> > type of list, I can only think of using gcc's typeof extesion, with
> > the help of the so called "substitution-failure-is-not-an-
> > error" (SFINAE).
>
> > Is there solution in the current C++ standard?
>
> I am not sure whether I completely understand your problem, but maybe
> something along the following lines contains ideas you could use:
>
> #include <list>
> #include <vector>
>
> template < typename T >
> struct rec_list_to_vector {
>
>    typedef T result_type;
>    typedef T argument_type;
>
>    static
>    result_type convert ( argument_type const & arg ) {
>      return ( arg );
>    }
>
> };
>
> template < typename T >
> struct rec_list_to_vector< std::list<T> > {
>
>    typedef
>    std::vector< typename rec_list_to_vector<T>::result_type >
>    result_type;
>
>    typedef
>    std::list< typename rec_list_to_vector<T>::argument_type >
>    argument_type;
>
>    static
>    result_type convert ( argument_type const & arg ) {
>      result_type result;
>      for ( typename argument_type::const_iterator iter = arg.begin();
>            iter != arg.end(); ++iter ) {
>        result.push_back( rec_list_to_vector<T>::convert( *iter ) );
>      }
>      return ( result );
>    }
>
> };
>
> template < typename T >
> typename rec_list_to_vector<T>::result_type
> convert_list_to_vector ( T const & t ) {
>    return ( rec_list_to_vector<T>::convert( t ) );
>
> }
>
> int main ( void ) {
>    std::list< std::list< int > > ivv;
>    std::vector< std::vector< int > > ill = convert_list_to_vector( ivv );
>
> }
>
> Best
>
> Kai-Uwe Bux

Ah...yeah, silly I am, didn't think of sepecialize ReturnType
itself...
Thanks very much, and thank Kevin too :-)


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply bood 8/31/2008 4:57:43 PM

3 Replies
231 Views

(page loaded in 0.084 seconds)


Reply: