POV-Ray : Newsgroups : povray.off-topic : C++ templates to get Java-style interfaces... : Re: C++ templates to get Java-style interfaces... Server Time
11 Oct 2024 09:19:20 EDT (-0400)
  Re: C++ templates to get Java-style interfaces...  
From: Warp
Date: 20 Oct 2007 16:57:06
Message: <471a6ba2@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> >   The idea with concepts is that you can create specify explicitly in the
> > declaration of a template function all the requirements for the template

> That's the basic idea behind Java-style "interface" declarations, you 
> see.  Hence, the subject line of the post.

  Java interfaces have little to do with template requirements.

  Can you, for example, create a function which says "the parameter type
has to support this interface specification of mine" and then give it an
*existing* type (which has never even heard about your requirement
specification)? No, because the existing class would have to explicitly
say "implements <the definition>", which is impossible if it's an existing
class which you can't modify.

  Can you, for example, create in Java a function which expects a parameter
which supports the + operator, and then you can call this function by giving
it an 'int' (the internal type, not the 'Integer' class) or a string? No,
because neither one says "implements <whatever the + operator interface
would be>". And especially no because 'int' has absolutely nothing to do
with a string, and thus you can't create a function in Java which would
accept either one as a parameter. They have nothing in common, not a common
base class, not a common interface, nothing at all.

  That's precisely what makes templates different from inheritance. They
basically accept anything. Want to call the function with an int? Then call
it giving it an int. Want to do the same with a string? Then go ahead. An
'int' and a 'string' don't need to have anything in common (except that both
support the '+' operator).

  Besides, Java interfaces are *implemented*, very specifically, template
parameter requirements are not. You don't have to tell for a type that
"it implements this template parameter requirement". You just give it
to the template function and it can (in the future C++ standard) check
that it indeed does so. Thus for example an internal type can conform
to such a specification (even one written by yourself) even though the
internal type has absolutely no knowledge of that specification.
  With Java interfaces the class must know the specification and must
implement it very explicitly with syntax for that specific purpose. It
has no way of implementing specifications which it cannot see.

> I was more amused by example of picking different operators to aid the 
> code generator in generating a more efficient operation for "+" than 
> anything, tho.

  I don't really understand what's so amusing with that. No compiler is
perfect (regardless of language).

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.