On Wed, Sep 19, 2012 at 11:39 AM, Lawrence Crowl <cr...@googlers.com> wrote:
> On 9/19/12, Gabriel Dos Reis <g...@integrable-solutions.net> wrote:
>> On Sep 19, 2012 Richard Guenther <richard.guent...@gmail.com> wrote:
>> > 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?).
>>
>> No, it is a language primitive.
>>
>> but we can define out own operation with similar syntax that allows
>> for specialization, whose generic implementation uses dynamic_cast.
>>
>>   template<typename T, typename U>
>>   T* is(U* u) {
>>       return dynamic_cast<T*>(u);
>>   }
>
> At this point, dynamic_cast is not available because we do not
> yet have polymorphic types.  There has been some resistance to
> that notion.
>
> Absent dynamic cast, we need to specialize for various type
> combinations.

The generic implementation can simply assert.

template<typename T> bool symbol::is()
{
  assert (0);
  return true;
}

template <> bool symbol::is<function>()
{
   if (type_ == FUNCTION)
     return true;
  return false;
}

template <> bool symbol::is<variable>()
{
   if (type_ == FUNCTION)
     return false;
  return true;
}

David

>  Function template specialization would be handy,
> but C++ does not directly support that.  We could work around
> that.  However, in the end, the fact that try_whatever is a member
> function means that we can use a notation that depends on context
> and so can be shorter.  That is, we can write 'function' instead of
> 'cgraph_node *'.
>
> --
> Lawrence Crowl

Reply via email to