41d7577e   
   From: daniel.kruegler@googlemail.com   
      
   Am 08.03.2013 07:22, schrieb Jerry:   
   > Hello to all of you. I have something I am sure you can help with.   
   > Consider this:   
   >   
   > struct rec   
   > {   
   > int id;   
   > char label[20];   
   > }   
   >   
   > rec array[26] = {rec( 1,"a"),rec( 2,"b")};   
      
   I assume you have additional constructors in rec that you have not shown   
   us here.   
      
   > template   
   > R get(R* begin,R* end,V R::* m,V v)   
   > {   
   > for(R* it=begin;it!=end;it++)   
   > if((*it).*m == v)   
   > return (*it);   
   > return R();   
   > }   
   >   
   > int main()   
   > {   
   > std::vector r1 = get(array,array+size,&rec::id,2);   
   > return 0;   
   > }   
   >   
   > Ok that works.   
      
   I don't understand how this should work, because it would mean that you   
   can implicitly convert an rvalue of type rec to std::vector. I'm   
   ignoring the initialization of the vector for the moment.   
      
   >But I can't do it with the other member like this:   
   >   
   > int main()   
   > {   
   > std::vector r1 = get(array,array+size,&rec::label,"b");   
   > return 0;   
   > }   
   >   
   > And the reason I can't is that get would have to be defined:   
   >   
   > template   
   > R get(R* begin,R* end,char R::* m[20],const char * v)   
   > {   
   > ...   
   > }   
   >   
   > Well I could but how many functions do I need since the size of the   
   > array must be specified in the function signature?   
      
   This shouldn't be much of a problem, since you can add a *single*   
   overload for array types, e.g.   
      
   template   
   R get(R* begin, R* end, V (R::* m)[N], const V(&v)[M]);   
      
   If you want to allow for the case that V is a decayed pointer, you might   
   want to try   
      
   template   
   R get(R* begin, R* end, V (R::* m)[N], const V* v);   
      
   > Is there any way to generically define this function so I can pass any   
   > structure member including char arrays of arbitrary length?   
   >   
   > Thanks for the advice. Also note that this compiler is over 10 years   
   > old and conforms to C++98 but can't compile any recent Boost library.   
      
   I assume that your compiler accepts *some* parts of boost. In this case   
   give a try to   
      
   #include "boost/type_traits/is_convertible.hpp"   
   #include "boost/type_traits/decay.hpp"   
   #include "boost/utility/enable_if.hpp"   
      
   template   
   typename boost::enable_if<   
    boost::is_convertible::type, U>,   
   R>::type   
   get(R* begin, R* end, V R::* m, U v)   
   {   
    for(R* it=begin;it!=end;it++)   
    if((*it).*m == v)   
    return (*it);   
    return R();   
   }   
      
   If it doesn't work, you could consider to implement simplified versions   
   of decay and is_convertible by yourself (shouldn't be too hard), or you   
   make a little be less generic, such as giving up the "convertible-to"   
   relation:   
      
   #include "boost/type_traits/is_same.hpp"   
   #include "boost/type_traits/decay.hpp"   
   #include "boost/utility/enable_if.hpp"   
      
   template   
   typename boost::enable_if<   
    boost::is_same::type, U>,   
   R>::type   
   get(R* begin, R* end, V R::* m, U v)   
   {   
    for(R* it=begin;it!=end;it++)   
    if((*it).*m == v)   
    return (*it);   
    return R();   
   }   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|