In a different thread, I proposed the following alternative to 'try_xxx': template<typename T> T* symbol::cast_to(symbol* p) { if (p->is<T>()) return static_cast<T*>(p); return 0; }
cast: template<typename T> T& symbol:as(symbol* p) { assert(p->is<T>()) return static_cast<T&>(*p); } David On Wed, Sep 19, 2012 at 2:17 AM, Richard Guenther <richard.guent...@gmail.com> wrote: > On Wed, Sep 19, 2012 at 9:29 AM, Eric Botcazou <ebotca...@adacore.com> wrote: >>> >>> The language syntax would bind the conditional into the intializer, as in >>> >>> if (varpool_node *vnode = (node->try_variable () >>> && vnode->finalized)) >>> varpool_analyze_node (vnode); >>> >>> which does not type-match. >>> >>> So, if you want the type saftey and performance, the cascade is really >>> unavoidable. >> >> Just write: >> >> varpool_node *vnode; >> >> if ((vnode = node->try_variable ()) && vnode->finalized) >> varpool_analyze_node (vnode); >> >> This has been the standard style for the past 2 decades and trading it for >> cascading if's is really a bad idea. > > Indeed. Btw, can we not provide a specialization for dynamic_cast <>? > This ->try_... looks awkward to me compared to the more familiar > > vnode = dynamic_cast <varpool_node> (node) > > but yeah - dynamic_cast is not a template ... (but maybe there is some > standard library piece that mimics it?). > > Richard. > >> -- >> Eric Botcazou