const Propagation

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 29 12:07:05 PST 2014


On Mon, 29 Dec 2014 16:48:45 +0000
Julian Kranz via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> Is that really cool? I mean, is wise to have the compiler treat 
> templates and non-templates differently? C++ has tons of such 
> inconsistencies which is the main reason I don't really like 
> C++...
ah. "templates are not functions!" ;-)

this is completely unpractical to forbid compiler doing attribute
inference on templates: it's not always possible to manually attribute
template. for example:

  void a(T) (T n) {
    ...some code 1
    static if (isThisSomething!T) {
      ...some code 2
    }
    ...some code 3
  }

  '...some code 2' may be used only for specific types and it can be
non-safe, and '...some code 1' and '...some code 2' is always safe. yet
we can't mark the template with `@safe` here, we must copy-paste the
whole template code: once for `@system` variant and once for `@safe`
variant. this will effectively kill the template handyness.

as for functions... function attributes are the part of it's signature.
see: `void a ();` is not the same function as `void a () @safe;`. they
has different signatures and different name mangling. this can't be
avoided, as linker has to link the correct implementation, and linker
has no clue about D, it sees only mangled function names. so
technically compiler can do attribute inference on functions, but
practically it is impossible, as attributes depends of the call site,
and compiler has to generate function signature BEFORE it is called.

that's why i'm emphasising "template is not XXX" sentense. template is
a template, it's not a function. don't let it trick you! ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141229/45bc6a8c/attachment.sig>


More information about the Digitalmars-d mailing list