On Tue, 28 Apr 2009 05:06:23 -0400, bearophile <[email protected]> wrote:

bearophile:
struct Vec(T=float, size_t N=3) {
    static assert(N > 0);

Sorry. Better to use:

struct Vec(T=float, int N=3) {
    static assert(N > 0);

From experience using D I have seen that using unsigned types is very unsafe in the current D language. In Delphi using unsigned types increases safety, in D actually decreases it. So in D it's better avoid unsigned type every time they aren't strictly necessary.

Bye,
bearophile

Well, in this case, N=-1 is both a compile time constant and doesn't compile (static arrays have a max size of 16kb).

Anyways, here's an alternate version:
import std.conv: text;
struct _Vec(T:U[N],U,size_t N) {
    T _data;
    alias _data this;
    string toString() { return text(_data); }
}

_Vec!(T) Vec(T)(T val)
    if( is(_Vec!(T)) )
{
    _Vec!(T) r = val;
    return r;
}

Which simplifies use:

auto foo(T,size_t N)(T[N] v) {
    // do stuff with v
    return Vec(v);
}

although functions as array parameters doesn't allow setters, i.e.:
pos.x;     // okay
pos.x = 5; // error

Reply via email to