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---
AMDG Olaf Peter wrote:
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;
template<typename T, T data::*mem_ptr> struct set_generic { explicit set_generic( actions& actions ) : self( actions ) { } void operator()(const T& data) const { (self.m_data.*mem_ptr) = data; } private: actions& self; }; set_generic<std::string, &data::text> set_text; In Christ, Steven Watanabe
participants (2)
-
Olaf Peter
-
Steven Watanabe