xmas79 wrote:Something like assigning a pointer in the structure to a local variable (common when working with strings).
C++ strings (as well as all standard containers) can handle that just fine. They have proper copy constructors.
Returning something that contains a pointer to a local variable is always a problem, and it has nothing to do with returning copies of structs.
You could have also assigned a pointer in the struct to a local variable before returning a pointer to that struct. The effect is exactly the same.
Why would you do something else before trying to use the hash results? When I get a hash hit I check if I can immediately take a cut-off, if not I try the hash move, and from this moment onward I don't look at this hash entry anymore. So am I missing something?
Yes, because you still have a copy of that pointer in scope, even though you are not allowed to touch it anymore. That is dangerous design. With very few exceptions (eg. explicit use of move semantics), in a well designed program, you should never have something in scope that you are not allowed to touch.
You may forget about this restriction in a few months, and start doing something with it.
For example, a common addition to the TT is to store whether it's worthwhile to do a null move search in this position. So you would write to the entry after doing a null move search.
You may remember to not do that now, but you probably won't in a few months time. You see a pointer, you see that it's not null, so you write to it.
And now you have an extremely subtle bug that you will probably never find. You have just set a flag in a hash table entry, that, with a 1/100000 chance, is actually for another position.
One way to get around that in this case is to never return the entry, and have the TTable code do the checking and return whether it's a hit or not. Returning a copy of the entry is safe, but potentially slower.
Disclosure: I work for DeepMind on the AlphaZero project, but everything I say here is personal opinion and does not reflect the views of DeepMind / Alphabet.