I’d like to create a variable which matches the type of another variable by way of a template such that if the other variable ever changes type to match, the one derived from it via a template also changes its type.
How can I do this with templates in C++?
The purpose is to ensure that when I read from disk into a temporary variable that the number of bytes read from disk exactly matches the actual variable. In this case, I am going to ignore the value so don’t want to read to the actual variable, but need to make sure I read the right number of bytes before moving on to keep things in sync.
If you don’t have decltype available in your compiler, you can write a template function to accomplish this. It’s kind of ugly but it will get the job done.
You’d call it like: