Pass in member variables of the specified class

I want to pass the name of a member variable. I thought I could do it with

template <typename T::*> void SetVal(T::* newval) { }; 

This obviously does not work, but hopefully goes through what I'm trying to do. I want to be able to set a specific member variable of a template template.

+4
source share
1 answer

You can always put a constant that defines compilation as template arguments. So here is what it would be:

 template <typename T, typename R, RT::* member> R& SetVal(T& t, const R& value) { t.*member = value; return t.*member; } struct A { int a; }; int main() { A a; SetVal<A,int,&A::a>(a, 10); return 0; } 
+2
source

Source: https://habr.com/ru/post/1300826/


All Articles