I have started learning about templates in C++ and am wondering if there is a way to say print out all the read and write to a particular variable just like we can do in CMake. For example, CMake has variable_watch() which is used to log all attempts to read or modify a variable.
So my question is that is there a way to do this in C++. If there is no built-in way(provided by the standard C++), then will it be possible to implement it somehow(if so, how?). I mean, if CMake can have this feature then maybe C++ too. Maybe we can make use of template metaprogramming or something else.
>Solution :
No, you cannot do it in C++ without wrapping your variable in something else that logs for you.
But it is possible to do what you need when you are debugging your code written in C++ using hardware breakpoints. You can use watch in gdb to check accesses to your variable. You can check it here or here. These types of breakpoints are available in Visual Studio too.