Q about template for matching arrays
Kirk McDonald
kirklin.mcdonald at gmail.com
Thu Feb 1 01:02:03 PST 2007
Bill Baxter wrote:
> I'd like to write a template function something like:
>
> T foo(T,S)(S x)
>
> that takes arrays of various kinds and returns an element of the base
> type (well really something derived from the base type, but that part's
> easy).
>
> The trick is that what I'd really like is for it be able to be used in
> all of the following ways:
>
> // IFTI
> foo(3) // T=int S=int
> foo([3]) // T=int S=int[]
> foo([[3]]) // T=int S=int[][]
> foo([[[3]]]) // T=int S=int[][][]
> ... etc
>
> // Base type explicit
> foo!(float)(3) // T=float S=float
> foo!(float)([3]) // T=float S=float[]
> foo!(float)([[3]]) // T=float S=float[][]
> ... etc
>
> It's easy if you say you have to call it like foo!(float[][])([[3]]),
> but that's annoying.
>
> Any bright ideas?
>
> --bb
This only works for dynamic arrays, but:
typeof(T.init[0]) foo(T)(T x) {}
--
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
More information about the Digitalmars-d-learn
mailing list