Re: How to pass a class by (const) reference to C++

2021-12-13 Thread evilrat via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 06:21:39 UTC, Tejas wrote: Hey, evilrat, I've seen people make claims that our C++ interop has reached phenomenal levels and that going any further would basically require a C++ compiler ala ImportC++, the issue is just that the docs haven't been updated yet to

Re: Passing a derived class where base class is defined as ref parameter

2021-12-13 Thread Mike Parker via Digitalmars-d-learn
On Monday, 13 December 2021 at 22:06:45 UTC, chopchop wrote: If I remove the ref, it works as expected, that is to say I can give a derived class as parameter. I have an idea why it does not work, but I think a c++ reference would work, ie incr(A& console) would accept a B as parameter. What t

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:08:30 UTC, evilrat wrote: On Monday, 13 December 2021 at 11:13:12 UTC, Tejas wrote: On Monday, 13 December 2021 at 09:21:26 UTC, Jan wrote: [...] You'll have to use something called a [shim](https://en.wikipedia.org/wiki/Shim_(computing)), it seems. For e

Re: Passing a derived class where base class is defined as ref parameter

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Monday, 13 December 2021 at 22:30:59 UTC, Adam D Ruppe wrote: On Monday, 13 December 2021 at 22:06:45 UTC, chopchop wrote: If I remove the ref, it works as expected, that is to say I can give a derived class as parameter. Why are you using the ref to begin with? What the logic here? Con

Re: Why code failed to compile for foo2?

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:43:51 UTC, Steven Schveighoffer wrote: On 12/11/21 10:02 PM, apz28 wrote: On Sunday, 12 December 2021 at 00:02:25 UTC, Stanislav Blinov wrote: @apz28, I can't figure out the intent here. To convert result of abs to an unsigned? The function logic works only f

Re: Why code failed to compile for foo2?

2021-12-13 Thread apz28 via Digitalmars-d-learn
On Saturday, 11 December 2021 at 23:44:59 UTC, Adam Ruppe wrote: On Saturday, 11 December 2021 at 23:17:17 UTC, Stanislav Blinov wrote: ? No. If it was unsatisfied constraint, the error would've shown that. And if you try to instantiate it, you'll see it is an unsatisfied constraint anyway. T

Re: A debug class has started

2021-12-13 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 13, 2021 at 10:43:14PM +, forkit via Digitalmars-d-learn wrote: > On Monday, 13 December 2021 at 21:13:25 UTC, H. S. Teoh wrote: > > > > What you should be doing is: > > > > return to!string(str[0 .. len]); > > > > Or just: > > > > return str[0 .. len].idup; [...] > oh..

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 21:13:25 UTC, H. S. Teoh wrote: What you should be doing is: return to!string(str[0 .. len]); Or just: return str[0 .. len].idup; T oh.. so many different ways...(to both produce the same bug, and also to produce the correct output). ... i

Re: Passing a derived class where base class is defined as ref parameter

2021-12-13 Thread Adam D Ruppe via Digitalmars-d-learn
On Monday, 13 December 2021 at 22:06:45 UTC, chopchop wrote: If I remove the ref, it works as expected, that is to say I can give a derived class as parameter. Why are you using the ref to begin with? What the logic here? Consider this: class C : A {} void incr(ref A a) { a = new C; }

Passing a derived class where base class is defined as ref parameter

2021-12-13 Thread chopchop via Digitalmars-d-learn
Hi guys, below a small piece of code, which does not compile because "b" of type B can not be passed as ref parameter to incr(ref A a). If I remove the ref, it works as expected, that is to say I can give a derived class as parameter. I have an idea why it does not work, but I think a c++ re

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Tim via Digitalmars-d-learn
On Monday, 13 December 2021 at 21:17:49 UTC, Jan wrote: Unfortunately no. Maybe the cast would even make it work, but I can't have "A" and "_A" in D as separate types, because the class is supposed to link to C++ functions and thus renaming it from A to _A breaks that. On the other hand I can't

Re: A debug class has started

2021-12-13 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:58:42 UTC, forkit wrote: immutable(char)[] replaceChar(char* str, ulong len, char ch1, char ch2) //snip return to!(immutable(char)[])(str); } You're calling a `to` on a char pointer, which, ostensibly, would look for null terminator. Which there may not

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Jan via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:16:03 UTC, Ola Fosheim Grøstad wrote: On Monday, 13 December 2021 at 12:08:30 UTC, evilrat wrote: Yeah but it sucks to have making C++ wrapper just for this. I think either pragma mangle to hammer it in place or helper dummy struct with class layout that mimics

Re: A debug class has started

2021-12-13 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 13, 2021 at 08:58:42PM +, forkit via Digitalmars-d-learn wrote: [...] > immutable(char)[] replaceChar(char* str, ulong len, char ch1, char ch2) > { > for (ulong i = 0; i < len; i++) > { > if (str[i] == ch1) > { > writefln("Found %c at str[%d]", ch

Re: A debug class has started

2021-12-13 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 13, 2021 at 08:47:12PM +, forkit via Digitalmars-d-learn wrote: > On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote: > > On Mon, Dec 13, 2021 at 08:04:24PM +, forkit via Digitalmars-d-learn > > wrote: [...] > > > (this produces an unpredictable result??) > > > char*

Re: ImportC std support

2021-12-13 Thread forkit via Digitalmars-d-learn
On Sunday, 12 December 2021 at 08:25:09 UTC, Dave P. wrote: ImportC is not ready for general use yet. I think you nailed it ;-) https://dlang.org/changelog/2.098.1.html

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote: On Mon, Dec 13, 2021 at 08:04:24PM +, forkit via Digitalmars-d-learn wrote: On Monday, 13 December 2021 at 12:06:53 UTC, WebFreak001 wrote: > > You should really use `.dup` if you want to mutate your > string. (You would need t

Re: unit test broken [DUB bug?]

2021-12-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/12/21 12:54 AM, Chris Katko wrote: Running 64-bit Linux ``` dmd --version DMD64 D Compiler v2.098.0-beta.2 dub --version DUB version 1.27.0-beta.2, built on Sep  7 2021 ``` the following code 'compiles' in one project. ```d unittest { gasdindgaslkdgansklnasgdlknaglkgansklsdg; } void mai

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote: On Mon, Dec 13, 2021 at 08:04:24PM +, forkit via Digitalmars-d-learn wrote: On Monday, 13 December 2021 at 12:06:53 UTC, WebFreak001 wrote: > > You should really use `.dup` if you want to mutate your > string. (You would need t

Re: Why code failed to compile for foo2?

2021-12-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/11/21 10:02 PM, apz28 wrote: On Sunday, 12 December 2021 at 00:02:25 UTC, Stanislav Blinov wrote: @apz28, I can't figure out the intent here. To convert result of abs to an unsigned? The function logic works only for unsigned type and the parameter value can be altered in body hence Unq

Re: ImportC std support

2021-12-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/11/21 4:42 PM, ManKey wrote: What implementations of the C standard library does importC support? ImportC isn't really a way to import C. It's a C compiler built into the D compiler. It only compiles C11 code *without* any headers (i.e. you can't use C standard library). In order to p

Re: A debug class has started

2021-12-13 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 13, 2021 at 08:04:24PM +, forkit via Digitalmars-d-learn wrote: > On Monday, 13 December 2021 at 12:06:53 UTC, WebFreak001 wrote: > > > > You should really use `.dup` if you want to mutate your string. (You > > would need to duplicate anyway if you don't want an unsafe cast) > > (

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:06:53 UTC, WebFreak001 wrote: You should really use `.dup` if you want to mutate your string. (You would need to duplicate anyway if you don't want an unsafe cast) (this produces an unpredictable result??) char* w = cast(char*)str.dup; (but this seems to wo

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Jan via Digitalmars-d-learn
On Monday, 13 December 2021 at 16:29:12 UTC, Tim wrote: I made a pull request, which changes the mangling to tail const for classes passed directly as parameter or return type, but now think this would break too much code: https://github.com/dlang/dmd/pull/13369 The proposal to add a deref-type

Re: unit test broken [DUB bug?]

2021-12-13 Thread Andre Pany via Digitalmars-d-learn
On Sunday, 12 December 2021 at 05:54:44 UTC, Chris Katko wrote: Running 64-bit Linux ``` dmd --version DMD64 D Compiler v2.098.0-beta.2 [...] I really recommend always using dub configurations, especially when you want to use unit tests. The name of the first configuration doesn't matter. I

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Tim via Digitalmars-d-learn
On Monday, 13 December 2021 at 15:21:19 UTC, Jan wrote: On Monday, 13 December 2021 at 13:02:50 UTC, Ola Fosheim Grøstad wrote: Yes, I wouldn't want to use it, maybe manual mangling is better, but still painful. ```const A&``` is so common in C++ API's that it really should be supported out-of-

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Jan via Digitalmars-d-learn
On Monday, 13 December 2021 at 13:02:50 UTC, Ola Fosheim Grøstad wrote: On Monday, 13 December 2021 at 12:51:17 UTC, evilrat wrote: That example is still looks very conspicuous because it is very likely does nothing on the caller side in C++ as it passes a copy. Yes, I wouldn't want to use it

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:51:17 UTC, evilrat wrote: That example is still looks very conspicuous because it is very likely does nothing on the caller side in C++ as it passes a copy. Yes, I wouldn't want to use it, maybe manual mangling is better, but still painful. ```const A&``` is

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread evilrat via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:16:03 UTC, Ola Fosheim Grøstad wrote: On Monday, 13 December 2021 at 12:08:30 UTC, evilrat wrote: Yeah but it sucks to have making C++ wrapper just for this. I think either pragma mangle to hammer it in place or helper dummy struct with class layout that mimics

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:08:30 UTC, evilrat wrote: Yeah but it sucks to have making C++ wrapper just for this. I think either pragma mangle to hammer it in place or helper dummy struct with class layout that mimics this shim logic is a better solution in such cases. Literally anything

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Monday, 13 December 2021 at 12:16:03 UTC, Ola Fosheim Grøstad wrote: class _A {} struct A {} With ```extern(C++)``` on these…

Re: A debug class has started

2021-12-13 Thread WebFreak001 via Digitalmars-d-learn
On Monday, 13 December 2021 at 11:09:18 UTC, drug wrote: On 13.12.2021 13:49, forkit wrote: On Monday, 13 December 2021 at 09:49:05 UTC, forkit wrote: char* w = cast(char*)str.toStringz; // this seems to be the solution class has ended ;-) That's because `str` is initialized by a li

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread evilrat via Digitalmars-d-learn
On Monday, 13 December 2021 at 11:13:12 UTC, Tejas wrote: On Monday, 13 December 2021 at 09:21:26 UTC, Jan wrote: On Monday, 13 December 2021 at 07:48:34 UTC, evilrat wrote: On Sunday, 12 December 2021 at 21:24:39 UTC, Jan wrote: In D I have an extern(C++) class: ```cpp extern(C++) class A {

Re: A debug class has started

2021-12-13 Thread drug via Digitalmars-d-learn
On 13.12.2021 14:26, ag0aep6g wrote: On 13.12.21 12:09, drug wrote: That's because `str` is initialized by a literal and you can not change it by definition. When you call `toStringz` it duplicates that literal (adding terminating zero at the end) and the duplicate is mutable. I would recommen

Re: A debug class has started

2021-12-13 Thread ag0aep6g via Digitalmars-d-learn
On 13.12.21 12:09, drug wrote: That's because `str` is initialized by a literal and you can not change it by definition. When you call `toStringz` it duplicates that literal (adding terminating zero at the end) and the duplicate is mutable. I would recommend do not use `toStringz` and just make

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:21:26 UTC, Jan wrote: On Monday, 13 December 2021 at 07:48:34 UTC, evilrat wrote: On Sunday, 12 December 2021 at 21:24:39 UTC, Jan wrote: In D I have an extern(C++) class: ```cpp extern(C++) class A { ~this(); // other stuff } ``` An a function that

Re: A debug class has started

2021-12-13 Thread drug via Digitalmars-d-learn
On 13.12.2021 13:49, forkit wrote: On Monday, 13 December 2021 at 09:49:05 UTC, forkit wrote: char* w = cast(char*)str.toStringz; // this seems to be the solution class has ended ;-) That's because `str` is initialized by a literal and you can not change it by definition. When you cal

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:49:05 UTC, forkit wrote: char* w = cast(char*)str.toStringz; // this seems to be the solution class has ended ;-)

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:49:05 UTC, forkit wrote: oh... Windows - dmd version is 2.098.0-dirty - ldc2 version is 1.28 (based on dmd v2.098.0) Linux - dmd version is 2.098 - ldc2 version is 1.20.1 (based on dmd v2.090.1)

A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
ok. one line in this code is causing a problem (as noted in comments) an explanation as to the cause, is welcome ;-) // module test; import std : writeln, writefln; import std.conv : to; void main() { string str = "abc;def;ab"; char* w = cast(char*)str; w

Re: template ctor overload Segmentation fault

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Sunday, 12 December 2021 at 19:17:53 UTC, vit wrote: On Sunday, 12 December 2021 at 18:32:28 UTC, Imperatorn wrote: On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: Hello, why does this code fail to compile? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this

Re: How to loop through characters of a string in D language?

2021-12-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 10 December 2021 at 18:47:53 UTC, Stanislav Blinov wrote: Threshold could be relative for short strings and absolute for long ones. Makes little sense reallocating if you only waste a couple bytes, but makes perfect sense if you've just removed pages and pages of semicolons ;) Like

Re: How to pass a class by (const) reference to C++

2021-12-13 Thread Jan via Digitalmars-d-learn
On Monday, 13 December 2021 at 07:48:34 UTC, evilrat wrote: On Sunday, 12 December 2021 at 21:24:39 UTC, Jan wrote: In D I have an extern(C++) class: ```cpp extern(C++) class A { ~this(); // other stuff } ``` An a function that takes A by const reference: ```cpp void CppFunc(const A&

Re: template ctor overload Segmentation fault

2021-12-13 Thread user1234 via Digitalmars-d-learn
On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: Hello, why does this code fail to compile? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this(ref scope typeof(this) rhs){ } } struct Bar{ Foo!int foo; } void main(){ } ``` error: Segmentation fault

Re: Why code failed to compile for foo2?

2021-12-13 Thread Tejas via Digitalmars-d-learn
On Sunday, 12 December 2021 at 03:02:28 UTC, apz28 wrote: On Sunday, 12 December 2021 at 00:02:25 UTC, Stanislav Blinov wrote: @apz28, I can't figure out the intent here. To convert result of abs to an unsigned? The function logic works only for unsigned type and the parameter value can be al