c++ - Returning a reference to data held by boost::any -


I got a map of any signal, called gmap, such as:

 < Code> studs :: map & lt; Std :: string, boost: any: * & gt; & Amp; GMAP = Desicitington (). Global value;   

This map is actually a reference to global value, I have examined the address in GDB. I have also got control over any signal (which is why they are pointers, I refer, but I am debugging now). Now, then I return it to the same method as I declare GMAP:

  return boost :: any_cast   

To view and promote the collection: Any tag [T & amp] [[GMAP [key]] As soon as I pop that stack frame, which is strange, Because this is:

Returns : If a pointer is passed, it gives a worthy indicator of successful content, otherwise the zero is returned If T is ValueType, then it gives a copy of the organized value, otherwise, if the reference of the T (potentially worthable) ValueType A reference, it gives the reference value held.

So, in my opinion, it should not be returned to the context of a local variable

It's weird, but I know it.

Ignoring this fact it says that if T is a reference, then he will return one; I passed it an indicator and found the correct indicator. But for any reason, I can not just tell about return value and returns. First of all, I had to store that indicator in a clear local (opposition to discontent), priority and returns.

  return * boost :: any_cast & lt; T & gt; (& Amp; gmap [key]);   

Result in wrong behavior

  T * ret = boost :: any_cast & lt; T & gt; (& Amp; gmap [key]); Return * ret;   

The result in the right behavior.

Do not know why, because AFAIK local has only been clearly declared in the example below. In addition, GMAP is now one:

  Maps & lt; Std :: string, boost :: any & gt;   

Because now I do not need an object.

Comments