digitalmars.D.learn - Combining variadic functions with class templates
- Sebastian Schuberth (28/28) Sep 30 2010 Hi,
- Simen kjaeraas (9/18) Sep 30 2010 This is not a constructor, but a malformed function. In D, constructors
- Sebastian Schuberth (21/23) Sep 30 2010 Doh, thanks, I should have already known that ... changing "Vector" to
- wrzosk (5/26) Sep 30 2010 Change
- Sebastian Schuberth (5/20) Sep 30 2010 Thanks, that works, but that syntax seems a little verbose to me. Is the...
- Pelle (2/20) Sep 30 2010 It is not. You can use auto v = Vec3f(1,1,1);
- Philippe Sigaud (18/22) Sep 30 2010 If you use a creation function, types and length are deduced
- bearophile (6/24) Sep 30 2010 This is interesting code, thank you. Others have already shown you the t...
Hi, I'm all new to D (coming from C++), and currently playing around with the language. I'm using DMD 2.049 and was expecting this to compile: struct Vector(alias N,T) { Vector(T[N] v ...) { data=v; } T data[N]; }; alias Vector!(3,float) Vec3f; void main() { Vec3f v(1,1,1); } But instead I get these errors: Building Release\ConsoleApp1.exe... main.d(3): found 'v' when expecting ')' main.d(3): semicolon expected, not '...' main.d(3): Declaration expected, not '...' main.d(8): unrecognized declaration Building Release\ConsoleApp1.exe failed! What's wrong with the above code? Can member functions / constructors not be variadic? Is is not allowed to use template argument N in static array variadic function arguments? Thanks in advance. -- Sebastian Schuberth
Sep 30 2010
Sebastian Schuberth <sschuberth gmail.com> wrote:Hi, I'm all new to D (coming from C++), and currently playing around with the language. I'm using DMD 2.049 and was expecting this to compile: struct Vector(alias N,T) { Vector(T[N] v ...) {This is not a constructor, but a malformed function. In D, constructors bear the name 'this'[1].What's wrong with the above code? Can member functions / constructors not be variadic? Is is not allowed to use template argument N in static array variadic function arguments?The error is, as said above, the name of your constructor. This leads to a less-than-readable error message, which ultimately is trying to tell you what I'm saying. [1]: http://digitalmars.com/d/2.0/struct.html#Struct-Constructor -- Simen
Sep 30 2010
On 30.09.2010 19:58, Simen kjaeraas wrote:This is not a constructor, but a malformed function. In D, constructors bear the name 'this'[1].Doh, thanks, I should have already known that ... changing "Vector" to "this" makes it better, but with struct Vector(alias N,T) { this(T[N] v ...) { data=v; } T data[N]; }; alias Vector!(3,float) Vec3f; void main() { Vec3f v(1,1,1); } I still get Building Release\ConsoleApp1.exe... main.d(14): found 'v' when expecting ';' following statement Building Release\ConsoleApp1.exe failed! -- Sebastian Schuberth
Sep 30 2010
W dniu 2010-10-01 03:13, Sebastian Schuberth pisze:On 30.09.2010 19:58, Simen kjaeraas wrote:Change Vec3f v(1,1,1); into Vec3f v = Vec3f(1,1,1);This is not a constructor, but a malformed function. In D, constructors bear the name 'this'[1].Doh, thanks, I should have already known that ... changing "Vector" to "this" makes it better, but with struct Vector(alias N,T) { this(T[N] v ...) { data=v; } T data[N]; }; alias Vector!(3,float) Vec3f; void main() { Vec3f v(1,1,1); } I still get Building Release\ConsoleApp1.exe... main.d(14): found 'v' when expecting ';' following statement Building Release\ConsoleApp1.exe failed!
Sep 30 2010
On 30.09.2010 20:20, wrzosk wrote:Thanks, that works, but that syntax seems a little verbose to me. Is the first syntax generally not supported in D? -- Sebastian Schuberthvoid main() { Vec3f v(1,1,1); } I still get Building Release\ConsoleApp1.exe... main.d(14): found 'v' when expecting ';' following statement Building Release\ConsoleApp1.exe failed!Change Vec3f v(1,1,1); into Vec3f v = Vec3f(1,1,1);
Sep 30 2010
On 09/30/2010 08:25 PM, Sebastian Schuberth wrote:On 30.09.2010 20:20, wrzosk wrote:It is not. You can use auto v = Vec3f(1,1,1);Thanks, that works, but that syntax seems a little verbose to me. Is the first syntax generally not supported in D?void main() { Vec3f v(1,1,1); } I still get Building Release\ConsoleApp1.exe... main.d(14): found 'v' when expecting ';' following statement Building Release\ConsoleApp1.exe failed!Change Vec3f v(1,1,1); into Vec3f v = Vec3f(1,1,1);
Sep 30 2010
If you use a creation function, types and length are deduced automatically, there is no need to indicate them to the compiler. Let's add some sugar on top, it complexifies the signature a bit but makes creating a vector a pleasure: import std.traits; auto vector(Ts...)(Ts values) if (!is(CommonType!(Ts) == void)) { return Vector!( CommonType!(Ts), Ts.length)(values); } (CommonType: http://digitalmars.com/d/2.0/phobos/std_traits.html#CommonType) usage: auto v1 = vector(1,2,3); // v1 is a Vector!(int,3) auto v2 = vector(1.5); // v2 is a Vector!(double, 1) auto v3 = vector(1,2, 3.1415, 2.71828); // v3 is a Vector!(double, 4): CommonType!(int,int,double,double) is double auto v4 = vector('a','b'); // Vector!(char,2) auto v5 = vector("abc", 3); // Compile-time error, impossible vector. PhilippeThanks, that works, but that syntax seems a little verbose to me. Is the first syntax generally not supported in D?It is not. You can use auto v = Vec3f(1,1,1);
Sep 30 2010
Sebastian Schuberth:I'm all new to D (coming from C++), and currently playing around with the language. I'm using DMD 2.049 and was expecting this to compile: struct Vector(alias N,T) { Vector(T[N] v ...) { data=v; } T data[N]; }; alias Vector!(3,float) Vec3f; void main() { Vec3f v(1,1,1); }This is interesting code, thank you. Others have already shown you the two bugs in your code, plus other ways to design it. At the end of struct/enum/class definitions don't put a semicolon, it's not needed in D. I have filed an enhancement request about those error messages: http://d.puremagic.com/issues/show_bug.cgi?id=4962 Bye, bearophile
Sep 30 2010