Fun with templates

Marco Leise Marco.Leise at gmx.de
Sun Jul 7 04:19:50 PDT 2013


Am Sun, 7 Jul 2013 00:06:26 +1000
schrieb Manu <turkeyman at gmail.com>:

> On 6 July 2013 22:27, Namespace <rswhite4 at googlemail.com> wrote:
> 
> > The way that makes the most sense to me is:
> >>
> >> void f(T)(Unqual!T t) {}
> >>
> >> Given an immutable(T) for instance that I want to call with, it would
> >> instantiate the template with the signature void f(T), and then attempt to
> >> call it with the immutable(T). If immutable(T) is not convertible to the
> >> argument T, then it would produce a compile error as if attempting to call
> >> any such function that just receives T.
> >>
> >> The point here is that I want more control over the signature of the
> >> instantiated template (reduce the number of permutations generated for
> >> various calls). Template blow-out is perhaps the biggest and most well
> >> known day-to-day problem in C++, and tools like this may be very valuable
> >> to mitigate the disaster.
> >>
> >
> > It seems that your code works if you put the Template Type explicit:
> > ----
> > import std.stdio;
> > import std.traits : Unqual;
> >
> > void foo(T)(Unqual!T a) {
> >         writeln(typeof(a).stringof, " <-> ", T.stringof);
> > }
> >
> > void main() {
> >         int a;
> >         const int b;
> >         immutable int c;
> >
> >         //foo(c); /// Error
> >         foo!(typeof(a))(a);
> >         foo!(typeof(b))(b);
> >         foo!(typeof(c))(c);
> > }
> > ----
> >
> 
> Indeed, hence my point that the type deduction is the key issue here.
> It should be possible... maybe a bit tricky though.

If you wanted to save on template instantiations for every
possible attribute combination, you are doing it wrong. Those
are already 3 duplicate templates with binary identical
functions foo(int a) in them, which makes me cry on the inside.

-- 
Marco



More information about the Digitalmars-d mailing list