On Sat, Oct 13, 2012 at 12:44 PM, Lawrence Crowl <cr...@googlers.com> wrote:

>> Thus, please re-use or follow existing concepts.
>
> Both are existing concepts.  What I proposed is a common technique
> for avoiding the cost of dynamic_cast when down casting in a class
> hierarchy.  Both concepts will work.  I proposed what I thought
> would be most convenient to programmers.  However, I will change
> to the other form unless someone objects.

Let me elaborate on your point.

The concept you are trying to implement is that of:
   (a) check whether a tree is of a certain kind;
   (b) if so return a pointer to the (sub-)object of that kind;
         otherwise a null pointer.

This is a standard idiom -- at least when using C++.

It can be implemented in various ways.  Your earlier attempt
try_xxx is one example.  Another common example, built into
the language is dynamic_cast.  The latter requires that classes
involved in this test be polymorphic (because the builtin implementation
needs to consult metadata that are already present with virtual
functions.)  Yet, another implementation is what is currently in GCC
more-or-less.

I think we should name the operation based on the abstract concept
we are implementing as opposed the builtin language implementation.
That is why I recommend is<> over dyn_cast<> or variations of it.

We should be able to understand its uses without having to know
the implementation details.


> However, I recommend against specializing on the enum, as it would
> become somewhat obscure when the hierarchy is discriminated on more
> than one enum.

100% agreed.

-- Gaby

Reply via email to