Difference between revisions of "DIP40"

From D Wiki
Jump to: navigation, search
(Abstract)
(Abstract)
Line 29: Line 29:
 
== Abstract ==
 
== Abstract ==
 
A proposed feature of C++14 is to introduce template parameter deduction for constructors, see [http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3602.html paper].
 
A proposed feature of C++14 is to introduce template parameter deduction for constructors, see [http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3602.html paper].
 +
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?
 +
 +
== Example ==
 +
<syntaxhighlight lang="d">
 +
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
 +
</syntaxhighlight>
 +
 +
== Extension ==
 +
A possible extension is to also allow template parameter deduction for static functions, using the same mechanism as for templates.
  
 
== Copyright ==
 
== Copyright ==

Revision as of 01:45, 13 May 2013

DIP 40: Template parameter deduction for constructors (do not read yet, still editing)

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. 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?

Example

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

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.