Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can we watch a read/write of/to a variable in C++

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading