DIP40
Contents
DIP 40: Template parameter deduction for constructors
Title: | Template parameter deduction for constructors. |
---|---|
DIP: | 40 |
Version: | 1 |
Status: | Draft |
Created: | 2013-05-12 |
Last Modified: | 2013-05-12 |
Author: | Timothee Cour |
Links: |
Abstract
A proposed feature of C++14 is to introduce template parameter deduction for constructors, see paper, mentioned here. The idea is to deduce template parameters when calling a constructor given the arguments given to the constructor, whenever possible. A compile error occurs when the deduction is ambiguous. The benefits would be:
- make the code more DRY
- make boilerplate of class instantiators unnecessary in most cases (they're all over phobos, eg: std.typecons.tuple, std.typecons.rebindable etc)
- make D more consistent: it deduces template parameters for functions, so why not for constructors, when this is unambiguous?
- it won't break any code.
Note, just as for deduction of normal functions, it should work with 0 or more template parameters specified (ie the first k>=0 templates may be provided).
Example
import std.typecons;
auto a=Tuple!(int, double)(1,1.0); //not DRY
auto a=tuple(1,1.0); //boilerplate in std.typecons: requires auxiliary class instantiator function 'tuple' just to allow this
auto a=Tuple(1,1.0); //proposed syntax that deduces type parameters
Another example:
struct A(T1)
if(!is(T1==float))
{
this(T2) if(!isNumeric!T2) (T2 a, T1 b){}
this()(T1 b){}
this()(){}
}
struct A(T1)
if(is(T1==float))
{
this()(){}
}
auto a=A(1,1.0); //deduced to A!(double)(1,1.0)
auto a=A(1.0); //deduced to A!(double)(1.0)
auto a=A(); //error: T1 cannot be deduced.
auto a=A(Object.init,1.0); //error: no matching type
Deduction
An overload set of constructors is formed containing all possible matching class/struct types with their template constraints (the 2 struct A definitions here) + constructors for each of those types. In our examples, this yields 3 + 1 = 4 constructors. Then the usual template deduction rules are used, taking into account the template constraints from the class/struct types and the ones from the constructors (as for normal functions). If there is an ambiguity or no match, give an error, otherwise instantiate.
Extension
A possible extension is to also allow template parameter deduction for static functions, using the same mechanism as for templates.
Copyright
This document has been placed in the Public Domain.