Is there a way to set an alias to specific form of a template?
Philippe Sigaud
philippe.sigaud at gmail.com
Sat Oct 8 00:49:31 PDT 2011
On Sat, Oct 8, 2011 at 08:43, Roderick Gibson <kniteli at gmail.com> wrote:
A possible problem (or feature ?) with the Vector(T...) code is that
you could possibly create a Vector!(int, float, string), for example.
Even with template constraints to limit the inner types to numerical
types, this template has too much freedom, I think : Vector!(float,
int, short) is a strange type for a vector.
I'd go the Vector(Type, int length) way myself. Or even Matrix(Type,
nRows, nCol) or somesuch.
> I got this working, found an odd bug though. This code,
>
> alias Vector!(float, float) vec2f;
> alias Vector!(double, double) vec2d;
> alias Vector!(float, float, float) vec3f;
>
> public struct Vector(T...) {
> int dim = 0;
>
> this(T...)(T args) {
> dim = args.length;
> }
>
> unittest {
> auto v = new vec2f(1.2f, 1.5f);
> vec2f d = new vec2f(1.1f, 1.4f);
>
> assert(v.dim == 2);
> assert(d.dim == 2);
>
> }
>
> will pass the first assert and fail on the second. Checking out the contents
> of v.length and d.length with writeln gives the correct answer on the first
> and
> 2
> 1
> RANDOMHEXCODE
> on the second. Very strange.
You Vector is a struct, a value type in D. Do not 'new' structs, as
you'd do in C++ (in D, you'll use new only for classes or reference
types in general)
Given a struct S, new S() has type S*. I think in:
vec2f d = new vec2f(...);
You create a vec2f and try to assign a vec2f* to it.
Try this, it should work:
unittest {
auto v = vec2f(1.2f, 1.5f);
vec2f d = vec2f(1.1f, 1.4f);
assert(v.dim == 2);
assert(d.dim == 2);
}
More information about the Digitalmars-d-learn
mailing list