Skip to content

Commit 9286c5f

Browse files
Emmanuel NyarkoEmmanuel Nyarko
authored andcommitted
C++ lifetime function handling documentation
calling C++ constructors in D are now possible and so must be updated in documentation
1 parent c53262f commit 9286c5f

File tree

1 file changed

+118
-11
lines changed

1 file changed

+118
-11
lines changed

spec/cpp_interface.dd

Lines changed: 118 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -933,17 +933,124 @@ $(H2 $(LNAME2 packing-and-alignment, Packing and Alignment))
933933

934934
$(H2 $(LNAME2 lifetime-management, Lifetime Management))
935935

936-
$(P C++ constructors, copy constructors, move constructors and destructors
937-
cannot be called directly in D code, and D constructors, postblit operators
938-
and destructors cannot be directly exported to C++ code. Interoperation of
939-
types with these special operators is possible by either 1$(RPAREN)
940-
disabling the operator in the client language and only using it in the host
941-
language, or 2$(RPAREN) faithfully reimplementing the operator in the
942-
client language. With the latter approach, care needs to be taken to ensure
943-
observable semantics remain the same with both implementations, which can be
944-
difficult, or in some edge cases impossible, due to differences in how the
945-
operators work in the two languages. For example, in D all objects are
946-
movable and there is no move constructor.)
936+
$(P C++ constructors, copy constructors, and destructors can be called directly in D code.
937+
C++ move constructors cannot be called directly in D code.
938+
)
939+
940+
$(P In a foo.cpp file: )
941+
942+
$(CPPLISTING
943+
#include $(LT)iostream$(GT)
944+
945+
using namespace std;
946+
947+
class A
948+
{
949+
public:
950+
A(int i);
951+
~A();
952+
};
953+
954+
A::A(int i)
955+
{
956+
cout << "calling C++ integer constructor " << endl;
957+
}
958+
959+
A::~A()
960+
{
961+
cout << "calling C++ destructor " << endl;
962+
}
963+
)
964+
965+
$(P In a bar.d file: )
966+
967+
------
968+
extern(C++) class A
969+
{
970+
this(int i);
971+
~this();
972+
}
973+
974+
void main()
975+
{
976+
auto obj1 = new A(5); // calls the constructor
977+
destroy!false(obj1); //calls the destructor
978+
}
979+
------
980+
981+
$(P Compiling, linking, and running produces the output:)
982+
983+
$(CONSOLE
984+
$(GT) g++ -c foo.cpp
985+
$(GT) dmd bar.d foo.o -L-lstdc++ && ./bar
986+
calling C++ integer constructor
987+
calling C++ destructor
988+
)
989+
990+
$(P Note that you cannot call C++ Copy constructor using D classes since classes in D are reference types.
991+
you need value semantics to be able to copy so you need to call them using D struct.)
992+
993+
$(P In a foo.cpp file: )
994+
995+
$(CPPLISTING
996+
#include $(LT)iostream$(GT)
997+
998+
using namespace std;
999+
1000+
class A
1001+
{
1002+
public:
1003+
A(int i);
1004+
A(A const& other);
1005+
~A();
1006+
};
1007+
1008+
A::A(int i)
1009+
{
1010+
cout << "calling C++ integer constructor" << endl;
1011+
}
1012+
1013+
A::A(A const& other)
1014+
{
1015+
cout << "calling C++ copy constructor" << endl;
1016+
}
1017+
1018+
A::~A()
1019+
{
1020+
cout << "calling C++ destructor" << endl;
1021+
}
1022+
)
1023+
1024+
$(P In a bar.d file: )
1025+
1026+
------
1027+
extern(C++, class) struct A
1028+
{
1029+
this(int i);
1030+
this(ref const A other);
1031+
~this();
1032+
}
1033+
1034+
void main()
1035+
{
1036+
A obj1 = 6; // calls the integer constructor
1037+
auto obj2 = A(obj1); // calls the copy constructor
1038+
}
1039+
------
1040+
1041+
$(P Compiling, linking, and running produces the output:)
1042+
1043+
$(CONSOLE
1044+
$(GT) g++ -c foo.cpp
1045+
$(GT) dmd bar.d foo.o -L-lstdc++ && ./bar
1046+
calling C++ integer constructor
1047+
calling C++ copy constructor
1048+
calling C++ destructor
1049+
calling C++ destructor
1050+
)
1051+
1052+
$(P Notice you don't need to call destroy on a struct object to invoke the destructor
1053+
since it does stack allocation and its lifetimes ends after leaving the stack.)
9471054

9481055
$(H2 $(LNAME2 special-member-functions, Special Member Functions))
9491056

0 commit comments

Comments
 (0)