Re: d strings are the bane of my existance

2021-12-06 Thread forkit via Digitalmars-d-learn

On Sunday, 5 December 2021 at 16:24:34 UTC, Chris Katko wrote:


I know there "is" a solution, it's just so odd to have this 
much difficulty using a string.


Paying attention to the online docs would help you too ;-)

https://dlang.org/library/std/socket/internet_address.this.html

But, in the event you do 'need' to work with strings, you can 
just convert the string to ushort.



// ---
module test;

import std.stdio : writeln;
import std.socket : InternetAddress;
import std.conv : to;

void main()
{
string host = "localhost";
string port = "8080";

auto ia = new InternetAddress(host, to!ushort(port));

writeln(ia);
}

//---



C++ bindings: const(T) dropped in return types of templates ?

2021-12-06 Thread Jan via Digitalmars-d-learn
I am trying to auto-generate bindings for C++ code in D. I've 
come across something that looks like a bug in DMD to me, but 
since I'm such a newbie in D maybe I am missing something.


My C++ class looks like this:

```cpp
template 
struct Vec3Template
{
  static const Vec3Template ZeroVector();
};
```

And my D binding code looks like this:

```cpp
extern(C++) struct Vec3Template(TYPE)
{
  static const(Vec3Template!(TYPE)) ZeroVector();
}
alias Vec3 = Vec3Template!(float);
```

However, when I try to use Vec3.ZeroVector() I am getting a 
linker error about unresolved symbols. It works with other 
functions, the error is specific to this function.


Now it complains that it can't find this one:
`?ZeroVector@?$Vec3Template@M@@SA?AU1@XZ`

However, I am using castXml to extract my C++ information, and 
that says that the mangled name should be:

`?ZeroVector@?$Vec3Template@M@@SA?BU1@XZ`

Running both names through undname.exe, an MSVC tool that 
generates the undecorated function name from the mangled name, it 
says that the latter function definition should be:
`public: static struct Vec3Template const __cdecl 
Vec3Template::ZeroVector(void)`


Whereas the former definition would be:
`public: static struct Vec3Template __cdecl 
Vec3Template::ZeroVector(void)`


So the one that D tries to link against is missing the `const`.

However, unless I misunderstood how to apply const to a type in 
D, you can see that I did wrap the type in const(). (I also tried 
immutable, but that's not allowed for extern C++ code).


So am I missing something, or did the compiler somehow forget 
about the const-ness?


I'm currently using DMD64 D Compiler v2.098.0-dirty


Re: How to check for overflow when adding/multiplying numbers?

2021-12-06 Thread Dave P. via Digitalmars-d-learn
On Monday, 6 December 2021 at 18:38:37 UTC, Ola Fosheim Grøstad 
wrote:

On Monday, 6 December 2021 at 17:46:35 UTC, Dave P. wrote:
I’m porting some C code which uses the gcc intrinsics to do a 
multiply/add with overflow checking. See 
[here](https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html) for reference. Is there a D equivalent?


There is:

https://dlang.org/phobos/core_checkedint.html

I have never used it, so I don't know how well it performs.


That seems to fit the bill.

Thanks!


Re: How to check for overflow when adding/multiplying numbers?

2021-12-06 Thread Ola Fosheim Grøstad via Digitalmars-d-learn

On Monday, 6 December 2021 at 17:46:35 UTC, Dave P. wrote:
I’m porting some C code which uses the gcc intrinsics to do a 
multiply/add with overflow checking. See 
[here](https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html) for reference. Is there a D equivalent?


There is:

https://dlang.org/phobos/core_checkedint.html

I have never used it, so I don't know how well it performs.




How to check for overflow when adding/multiplying numbers?

2021-12-06 Thread Dave P. via Digitalmars-d-learn
I’m porting some C code which uses the gcc intrinsics to do a 
multiply/add with overflow checking. See 
[here](https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html) for reference. Is there a D equivalent?


Re: d strings are the bane of my existance

2021-12-06 Thread Steven Schveighoffer via Digitalmars-d-learn

On 12/5/21 11:24 AM, Chris Katko wrote:

All I want:

```d
string ip_address = "192.168.1.1";
auto x = new InternetAddress( ip_string, "8008");
```

```d
source/app.d(161,16): Error: none of the overloads of `this` are 
callable using argument types `(string, int)`

`std.socket.InternetAddress.this(scope const(char)[] addr, ushort port)`
```


I know you have solved your problem, but take a look at the error 
message here -- "not callable using argument types `(string, int)`", 
whereas your sample has types `(string, string)`. It always helps to 
post *exactly* the code that is causing the problem, not code that you 
thought you used. If you want to make it simpler for posting, test the 
simpler code.


I'm assuming to get that error you did something like:

```d
auto port = 8008;
auto x = new InternetAddress( ip_string, port);
```

This doesn't work, because port is inferred to be `int`, not `ushort`. 
You can fix by using `ushort` instead of `auto`, or using the literal 
right in the call.


-Steve


Re: d strings are the bane of my existance

2021-12-06 Thread Ivan Kazmenko via Digitalmars-d-learn

On Sunday, 5 December 2021 at 16:37:21 UTC, Chris Katko wrote:
Yes! Thank you! I just realized the latter part was broken when 
I switched to using a uint for the addr. But I didn't know 
string is an alias for immutable(char)[]! Thank you!


Yeah, a `const(char)[]` argument is designed to accept both 
`immutable(char)[]` (strings) and just `char[]` (mutable arrays 
of chars) for the arguments.


Ivan Kazmenko.