On Friday, 24 July 2015 at 03:12:43 UTC, Steven Schveighoffer wrote:
On 7/23/15 9:30 PM, Enjoys Math wrote:
[...]

You're approaching this wrong. Do the lookup before deciding whether to instantiate a new object:

static Grammar getGrammar(const T[] str) {
     if(auto x = str in grammarCache)
         return *x;
     else
     {
         auto g = new Grammar;
         grammarCache[str] = g;
         return g;
     }
}

If you always want to dup, then do it outside the lookup. Don't do it in the constructor, you already have an object by then.

-Steve

Thanks.  That sounds like a good approach

Reply via email to