Re: in / out for C++ programmers

2012-03-23 Thread Jonathan M Davis
On Friday, March 23, 2012 23:37:27 Ali Çehreli wrote: > 'in' is the same as 'const scope'. const part is easy to understand but > I don't know what 'scope' does. I could not understand what the spec > means with "references in the parameter cannot be escaped (e.g. assigned > to a global variable)"

Re: in / out for C++ programmers

2012-03-23 Thread Ali Çehreli
On 03/23/2012 11:28 PM, Dan wrote: > When would you use in / out parameters instead of ref & const keywords? I am just about finished translating a chapter exactly on that topic. I've just realized that I have some questions myself. :) 'out' is the equivalent of a reference in C++, with additi

Re: in / out for C++ programmers

2012-03-23 Thread Jonathan M Davis
On Saturday, March 24, 2012 07:28:08 Dan wrote: > When would you use in / out parameters instead of ref & const > keywords? out sets the variable to its init value, so you don't run into issues with the behavior of the function changing based on the value of the variable that you passed in. out

in / out for C++ programmers

2012-03-23 Thread Dan
When would you use in / out parameters instead of ref & const keywords? Thanks.

Re: Map with maintained insertion order

2012-03-23 Thread Brad Anderson
On Saturday, 24 March 2012 at 01:07:56 UTC, Jonathan M Davis wrote: On Friday, March 23, 2012 23:48:51 Andrej Mitrovic wrote: Does someone have a map implementation that maintains the insertion order of the keys? E.g.: string[string] map; map["foo"] = "x"; map["bar"] = "y"; When iterating ov

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/24/12, Andrej Mitrovic wrote: > Correction, it's: > template isValue(T) > { > enum bool isValue = is(typeof( { Value v; T t; v = t; } )) > || is(typeof( { BaseElement!Value v; T t; v = t; } )) > || is(BaseElement!Value == T); > } Last check not needed, so: > template isValue(T) >

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/24/12, Andrej Mitrovic wrote: > static if (isArray!Value) > { > template isValue(T) > { >enum bool isValue = is(typeof( { Value v; T t; v = t; } )) || > is(BaseElement!Value == T); > } > } Correction, it's: template isValue(T) { enum bool isValue = is(typeof( { Value

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/24/12, Andrej Mitrovic wrote: > On 3/23/12, Andrej Mitrovic wrote: >> Does someone have a map implementation that maintains the insertion >> order of the keys? > > Here's a sloppy implementation: I forgot to make opIndex ref, but also the isValue check fails on structs with alias this, it's

Re: Map with maintained insertion order

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 06:33:54PM -0700, H. S. Teoh wrote: > On Fri, Mar 23, 2012 at 06:07:42PM -0700, Jonathan M Davis wrote: > > On Friday, March 23, 2012 23:48:51 Andrej Mitrovic wrote: > > > Does someone have a map implementation that maintains the insertion > > > order of the keys? > > > > >

Re: Map with maintained insertion order

2012-03-23 Thread H. S. Teoh
On Fri, Mar 23, 2012 at 06:07:42PM -0700, Jonathan M Davis wrote: > On Friday, March 23, 2012 23:48:51 Andrej Mitrovic wrote: > > Does someone have a map implementation that maintains the insertion > > order of the keys? > > > > E.g.: > > > > string[string] map; > > map["foo"] = "x"; > > map["bar

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/24/12, Jonathan M Davis wrote: > I can't think of any data structure that does that off the top of my head Java has it and they call it a LinkedHashMap: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashMap.html > That _does_ require having two data structures in one, and the

Re: Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, Andrej Mitrovic wrote: > Does someone have a map implementation that maintains the insertion > order of the keys? Here's a sloppy implementation: module test; import std.stdio; import std.traits; template KeyType(V : V[K], K) if (isAssociativeArray!(V[K])) { alias K KeyType

Re: Map with maintained insertion order

2012-03-23 Thread Jonathan M Davis
On Friday, March 23, 2012 23:48:51 Andrej Mitrovic wrote: > Does someone have a map implementation that maintains the insertion > order of the keys? > > E.g.: > > string[string] map; > map["foo"] = "x"; > map["bar"] = "y"; > > When iterating over map keys I want to visit "foo" first, then "bar",

Re: Freeing memory allocated at C function

2012-03-23 Thread Pedro Lacerda
On Fri, Mar 23, 2012 at 3:43 AM, Ali Çehreli wrote: > You can register on GC if you wrap the resources in a class. Then the > class object's destructor would call the clean up code. The problem is, it > is undeterministic when the destructor will be called, or will it be called > at all! > > Or y

Re: Confused by refusal to expand template

2012-03-23 Thread Timon Gehr
On 03/23/2012 11:58 PM, David wrote: Am 23.03.2012 23:52, schrieb H. S. Teoh: Code: struct S { int f(K)(K x) { return 1; } void func(K)(inout(K) x) { auto h = f(x); } } void main() { S s; s.func("abc"); // This is line 44 } This refuses to compile: test2.d(44): Error: template test2.S.func(K)

Re: Confused by refusal to expand template

2012-03-23 Thread David
Am 23.03.2012 23:52, schrieb H. S. Teoh: Code: struct S { int f(K)(K x) { return 1; } void func(K)(inout(K) x) { auto h = f(x); } } void main() {

Re: Confused by refusal to expand template

2012-03-23 Thread Timon Gehr
On 03/23/2012 11:52 PM, H. S. Teoh wrote: Code: struct S { int f(K)(K x) { return 1; } void func(K)(inout(K) x) { auto h = f(x); } } void main() {

Confused by refusal to expand template

2012-03-23 Thread H. S. Teoh
Code: struct S { int f(K)(K x) { return 1; } void func(K)(inout(K) x) { auto h = f(x); } } void main() { S s; s.func("abc"); // Th

Map with maintained insertion order

2012-03-23 Thread Andrej Mitrovic
Does someone have a map implementation that maintains the insertion order of the keys? E.g.: string[string] map; map["foo"] = "x"; map["bar"] = "y"; When iterating over map keys I want to visit "foo" first, then "bar", and so on.

Re: Template constraint and specializations

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, Philippe Sigaud wrote: > It works for me Yes but check the isA template. It seems there's something causing a nested variadic template to fail. This won't work in a template constraint (it returns false): template isA(alias Foo) { template isA(T) { enum bool isA = __trai

Re: Template constraint and specializations

2012-03-23 Thread Philippe Sigaud
On Fri, Mar 23, 2012 at 21:27, Andrej Mitrovic wrote: > That can't work. For a Foo!int your code will expand like so: > See for yourself: ? It works for me: template isBar(T) { enum isBar = __traits(compiles, { vo

Re: string[] to char**

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, bearophile wrote: > This is one way to do it: > immutable(char)** p = array(map!toStringz(data)).ptr; This is asked so frequently that I think we could consider adding it to Phobos.

Re: Template constraint and specializations

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, Philippe Sigaud wrote: > testFoo is a function that accepts any Foo!( ... ) for any ... The > second line tests it on a value of type T (T.init). That can't work. For a Foo!int your code will expand like so: // original code void tester(Args...)(Foo!Args args); tester(T.init); void

Re: string[] to char**

2012-03-23 Thread Ali Çehreli
On 03/23/2012 11:23 AM, Ali Çehreli wrote: On 03/23/2012 08:48 AM, simendsjo wrote: > What's the best way to convert char** from string[]? In C, char** communicates transfer of ownership. Ok, I once again misunderstood the question. :( My question in the comment remains. Thank you, Ali

Re: string[] to char**

2012-03-23 Thread Ali Çehreli
On 03/23/2012 08:48 AM, simendsjo wrote: > What's the best way to convert char** from string[]? In C, char** communicates transfer of ownership. Is that what you are trying to do? Are you going to pass a slice to a C function to be filled in by that C function? Such functions usually assign t

Re: Template constraint and specializations

2012-03-23 Thread Philippe Sigaud
On Fri, Mar 23, 2012 at 10:17, Ed McCardell wrote: >>> Is there a way to write a template constraint that matches any >>> >>> specialization of a given type? >> >> >> Nope. But there are simple workarounds: >> >> class Foo(bool feature1, bool feature2) { enum _isFoo = true; } >> >> template isFoo

Re: string[] to char**

2012-03-23 Thread bearophile
On Friday, 23 March 2012 at 15:48:12 UTC, simendsjo wrote: What's the best way to convert char** from string[]? This is one way to do it: import std.algorithm, std.array, std.string, core.stdc.stdio; void main() { auto data = ["red", "yellow", "green"]; immutable(char)** p = array(map

string[] to char**

2012-03-23 Thread simendsjo
What's the best way to convert char** from string[]?

Re: Calling c shared library

2012-03-23 Thread simendsjo
On Fri, 23 Mar 2012 15:04:48 +0100, simendsjo wrote: Forgive my programming 101 question :) I want to call a method from a precompiled shared library: // c header void f(void); // my d file extern(C) void f(); void main() {} $ dmd mydfile.d libphobos2.a(deh2_33a_525.o): In function `_D2rt4

Re: Vector operations optimization.

2012-03-23 Thread Comrad
On Friday, 23 March 2012 at 11:20:59 UTC, Trass3r wrote: The flags you want are -O, -inline -release. If you don't have those, then that might explain some of the slow down on slicing, since -release drops a ton of runtime checks. -noboundscheck option can also speed up things. dmd is anyw

Re: Vector operations optimization.

2012-03-23 Thread Comrad
On Friday, 23 March 2012 at 10:48:55 UTC, Dmitry Olshansky wrote: On 23.03.2012 9:57, Comrad wrote: On Thursday, 22 March 2012 at 10:43:35 UTC, Trass3r wrote: What is the status at the moment? What compiler and with which compiler flags I should use to achieve maximum performance? In general

Calling c shared library

2012-03-23 Thread simendsjo
Forgive my programming 101 question :) I want to call a method from a precompiled shared library: // c header void f(void); // my d file extern(C) void f(); void main() {} $ dmd mydfile.d libphobos2.a(deh2_33a_525.o): In function `_D2rt4deh213__eh_finddataFPvZPS2rt4deh213DHandlerTable': src/r

Re: Template constraint and specializations

2012-03-23 Thread bearophile
Andrej Mitrovic: Nope. But there are simple workarounds: Why isn't something similar to this working? import std.traits: Unqual; class Foo(bool feature1, bool feature2) {} template isFoo(T) { static if (is(Unqual!T Unused : Foo!Features, Features...)) { enum isFoo = true; }

Re: Vector operations optimization.

2012-03-23 Thread Trass3r
The flags you want are -O, -inline -release. If you don't have those, then that might explain some of the slow down on slicing, since -release drops a ton of runtime checks. -noboundscheck option can also speed up things.

Re: Vector operations optimization.

2012-03-23 Thread Dmitry Olshansky
On 23.03.2012 9:57, Comrad wrote: On Thursday, 22 March 2012 at 10:43:35 UTC, Trass3r wrote: What is the status at the moment? What compiler and with which compiler flags I should use to achieve maximum performance? In general gdc or ldc. Not sure how good vectorization is though, esp. auto-ve

Re: Template constraint and specializations

2012-03-23 Thread Ed McCardell
On 03/23/2012 04:14 AM, Andrej Mitrovic wrote: On 3/23/12, Ed McCardell wrote: Is there a way to write a template constraint that matches any specialization of a given type? Nope. But there are simple workarounds: class Foo(bool feature1, bool feature2) { enum _isFoo = true; } template isFo

Re: Template constraint and specializations

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, Ed McCardell wrote: > Is there a way to write a template constraint that matches any > specialization of a given type? Nope. But there are simple workarounds: class Foo(bool feature1, bool feature2) { enum _isFoo = true; } template isFoo(T) { enum bool isFoo = __traits(hasMember

Template constraint and specializations

2012-03-23 Thread Ed McCardell
Is there a way to write a template constraint that matches any specialization of a given type? For example can the following be done without having to write out every combination of feature1 and feature2: class Foo(bool feature1, bool feature2) { ... } void useFoo(T)(T foo) if (is(T ==