title: lazy pointer in C++
body: <p>I'm looking for a way to define a lazy pointer such that its data, say <code>ptr[i]</code>, is only generated when it is called. That is, before calling <code>ptr[i]</code>, the data is not in the memory or any other place. When <code>ptr[i]</code> is called, a callback function should be involved and get the value of <code>ptr[i]</code>.</p>
  <p>I want this pointer for I need to pass it to a C-style function in a third-party library(e.g. <code>mean(double * ptr, size_t n)</code> for computing mean value of a vector), so it must be a pointer and cannot be of any other type, but the data(possibly just random data for simulation) is extremely large and cannot be fit into the memory. For example, I want to simulate 100GB random double values and pass them to a mean function to compute its mean value and repeat the simulations 100 times.</p>
  <p>The idea of lazy pointers may sound wired but it should be possible since I know it can be implemented through the virtual file system and the memory-mapped file. For example, I can define a few callback functions to get a virtual disk. The files in my virtual disk look like real files but actually its data is generated by my callback functions. Then I can use the memory-mapped file to get a pointer to the virtual file. By doing that all the call for <code>ptr[i]</code> will be handled by the system and passed to my predefined callback functions. Therefore I can get a lazy pointer out of it. However, this implementation is complicated than I expect and requires dependencies(<code>Dokan</code> for windows and <code>fuse</code> for Linux). I hope there is a simpler way to do it.</p>