Boost logo

Boost Users :

From: Olaf Peter (ope-devel_at_[hidden])
Date: 2008-03-26 15:39:58


Hello,

how can I make the snipped more generic in that way, that I can use it
with different data members, e.g. as template argument the
&actions::m_data::text data address. Then I could write e.g.

set_generic<&actions::m_data::text, std::string> set_text;

Thanks,
Olaf

---8<---
#include <iostream>
#include <string>

struct data {
     std::string text;
};

struct actions
{
     explicit actions(data& d)
         : set_text( *this ),
           m_data( d )
     { }

     template<typename T>
     struct set_generic
     {
         explicit set_generic( actions& actions )
             : self( actions )
         { }
         void operator()(const T& data) const {
             self.m_data.text = data;
         }

     private:
         actions& self;
     };

     set_generic<std::string> set_text;
     set...

     data& m_data;
};

using namespace std;

int main()
{
     data d;
     actions a( d );

     a.set_text( "Hello" );

     cout << d.text << endl;
}

--->8---


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net