extern(C++) with template produces wrong mangleof

2016-12-02 Thread Timothee Cour via Digitalmars-d-learn
What's the difference bw _Z21test_D20161202T141925IiET_S0_ and
_Z21test_D20161202T141925IiEii? mangleof produces the one that's not in the
library produced:

test.cpp:
```
template
T test_D20161202T141925(T a);
template int test_D20161202T141925(int);
```

clang++ -shared -o libtest.so test.cpp

c++filt _Z21test_D20161202T141925IiET_S0_
int test_D20161202T141925(int)

c++filt _Z21test_D20161202T141925IiEii
int test_D20161202T141925(int)

nm libtest.so|grep test_D20161202T141925:
0ae0 W _Z21test_D20161202T141925IiET_S0_
```

test.d:
```
extern(C++){
  void test_D20161202T141743();
  T test_D20161202T141925(T)(T a);
}

void test(){
  assert(test_D20161202T141925!int.mangleof ==
"_Z21test_D20161202T141925IiEii");
// but I would expect _Z21test_D20161202T141925IiET_S0_ (otherwise,
undefined symbol)
}
```


Re: Delegates: Print 0..9

2016-12-02 Thread Timon Gehr via Digitalmars-d-learn

On 01.12.2016 21:12, Ali Çehreli wrote:



This is a common issue with D and some other languages (as I had learned
during a Dart language presentation, of which Dart does not suffer
from). All those delegates do close on the same loop variable. You need
to produce copies of the variable.


This is a common misconception. It's an implementation bug. The 
variables /are/ distinct. It is actually memory corruption. Evidence:


import std.stdio;

void main(){
int delegate()[] dgs;
foreach(immutable i;0..10){
dgs~=()=>i; // i is immutable
}
foreach(dg;dgs) writeln(dg()); // yet changes
dgs=[];
foreach(i;0..10){
int j=i; // creating a new variable
dgs~=()=>j;
}
foreach(dg;dgs) writeln(dg()); // does not help
}



Re: the best language I have ever met(?)

2016-12-02 Thread Igor Shirkalin via Digitalmars-d-learn
On Monday, 28 November 2016 at 16:15:23 UTC, Jonathan M Davis 
wrote:
That's what pragma(inline, true) is for. And if someone wants a 
different solution that's completely compile-time and doesn't 
work with variables, then fine. I'm talking about adding 
something to the standard library, and for that, I think that a 
solution that is as close as possible to being identical to 
simply declaring the static array with the length is what would 
be appropriate.



...


I'm not married to the syntax. I tried that syntax, but I 
couldn't figure out how to get it to work with runtime values.


Can I insert my own opinion about static arrays with programmers' 
unknown size?
Being the practical developer I can't imagine the situation where 
it is really needed. I hope D is not for theoretical goals only 
but for practical ones first.
If we know the size of our future array we tell that to the 
compiler. If we don't know the size of our future static array we 
write an external generator to produce that. I really don't know 
places where I want static array to be unknown size inline 
(perhaps, except for debugging purposes). Concluding, the 
designer knows he's achived the perfection if there is nothing to 
remove, but not to add.


Igor Shirkalin



Re: how to catch D Throwables (or exceptions) from C++?

2016-12-02 Thread Elie Morisse via Digitalmars-d-learn

On Friday, 2 December 2016 at 08:13:51 UTC, Jacob Carlborg wrote:
On 2016-12-01 02:58, Timothee Cour via Digitalmars-d-learn 
wrote:

eg:

```
dlib.d:
extern(C) void dfun(){assert(0, "some_msg");}

clib.cpp:
extern "C" void dfun();
void fun(){
  try{
dfun();
  }
  catch(...){
// works but how do i get "some_msg" thrown from D?
  }
}
```


At least for a C++ exception it's possible to get the current 
exception with __cxxabiv1::__cxa_current_primary_exception(). I 
verified and it works for C++ exceptions. I would think that 
the following works for D exceptions, but I cannot even catch 
the D exception in C++. Maybe it's not working properly on 
macOS.


// c++
void foo();
const char* getExceptionMessage(void*);

void bar()
{
try
{
foo();
}
catch(...)
{
void* e = __cxxabiv1::__cxa_current_primary_exception();
if (e)
{
const char* msg = getExceptionMessage(e);
if (msg)
printf("%s\n", msg);
else
printf("no message\n");
}
else
{
printf("no exception\n");
}
}
}

// d

extern(C++) void foo()
{
throw new Exception("foo");
}

extern(C++) immutable(char)* getExceptionMessage(void* e)
{
if (e)
{
auto t = cast(Throwable) e;
return t.msg.ptr;
}

return null;
}

I'm compiling the C++ code with clang++ and I need to link with 
libc++abi.


Exceptions thrown from D set a different exception class in the 
header, and the C++ personality routine (i.e the function that 
gets called for each catch block) only handles exceptions which 
displays the C++ exception class, so let D exceptions slip 
through.


To catch exceptions thrown by D code in C++ I think this would 
have to be done by throwing a C++ exception (eventually wrapping 
a D class), but this is yet to be implemented.


Re: how to catch D Throwables (or exceptions) from C++?

2016-12-02 Thread Jacob Carlborg via Digitalmars-d-learn

On 2016-12-01 02:58, Timothee Cour via Digitalmars-d-learn wrote:

eg:

```
dlib.d:
extern(C) void dfun(){assert(0, "some_msg");}

clib.cpp:
extern "C" void dfun();
void fun(){
  try{
dfun();
  }
  catch(...){
// works but how do i get "some_msg" thrown from D?
  }
}
```


At least for a C++ exception it's possible to get the current exception 
with __cxxabiv1::__cxa_current_primary_exception(). I verified and it 
works for C++ exceptions. I would think that the following works for D 
exceptions, but I cannot even catch the D exception in C++. Maybe it's 
not working properly on macOS.


// c++
void foo();
const char* getExceptionMessage(void*);

void bar()
{
try
{
foo();
}
catch(...)
{
void* e = __cxxabiv1::__cxa_current_primary_exception();
if (e)
{
const char* msg = getExceptionMessage(e);
if (msg)
printf("%s\n", msg);
else
printf("no message\n");
}
else
{
printf("no exception\n");
}
}
}

// d

extern(C++) void foo()
{
throw new Exception("foo");
}

extern(C++) immutable(char)* getExceptionMessage(void* e)
{
if (e)
{
auto t = cast(Throwable) e;
return t.msg.ptr;
}

return null;
}

I'm compiling the C++ code with clang++ and I need to link with libc++abi.

--
/Jacob Carlborg