Skip to content

Commit 36db760

Browse files
authored
Update to special member functions ddoc in c++ interop (#3820)
Update to special member functions ddoc in c++ interop Signed-off-by: Nicholas Wilson <thewilsonator@users.noreply.github.com> Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
1 parent 40461b0 commit 36db760

File tree

1 file changed

+111
-5
lines changed

1 file changed

+111
-5
lines changed

spec/cpp_interface.dd

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,119 @@ $(H2 $(LNAME2 lifetime-management, Lifetime Management))
951951
operators work in the two languages. For example, in D all objects are
952952
movable and there is no move constructor.)
953953

954-
$(H2 $(LNAME2 special-member-functions, Special Member Functions))
955-
956-
$(P D cannot directly call C++ special member functions, and vice versa.
957-
These include constructors, destructors, conversion operators,
958-
operator overloading, and allocators.
954+
$(H2 $(LNAME2 operator-overloading, Operator Overloading))
955+
$(P D's operator overloading is primarily involved with overloading its unary
956+
and binary operators including the increment, index, slice, cast, function call,
957+
comparison operator etc. Overloads of these operators that exist in C++ can be
958+
directly called from D provided the D operator and its C++ counterpart semantically agree.
959+
Below is a simple use case involving the assignment, index, and function call operator
960+
overloads interoperating between D and C++.
959961
)
960962

963+
$(P In a foo.cpp file: )
964+
965+
$(CPPLISTING
966+
#include $(LT)iostream$(GT)
967+
968+
class A
969+
{
970+
public:
971+
int a;
972+
int b;
973+
int buffer[3];
974+
A()
975+
{
976+
a = 0;
977+
b = 0;
978+
}
979+
A(int a, int b);
980+
A& operator=(const A& obj); //overloading the assignment operator
981+
void operator()(int a, int b, int c); // overloading the call operator
982+
int& operator[](int value); // overloading the index operator
983+
void printbuffer();
984+
};
985+
986+
A::A(int a, int b)
987+
{
988+
this->a = a;
989+
this->b = b;
990+
}
991+
992+
A& A::operator=(const A& obj)
993+
{
994+
a = obj.a;
995+
b = obj.b;
996+
return *this;
997+
}
998+
999+
void A::operator()(int a, int b, int c)
1000+
{
1001+
std::cout << "value of a is "<< a << std::endl;
1002+
std::cout << "value of b is "<< b << std::endl;
1003+
std::cout << "value of c is "<< c << std::endl;
1004+
}
1005+
1006+
int& A::operator[](int value)
1007+
{
1008+
return buffer[value];
1009+
}
1010+
1011+
void A::printbuffer()
1012+
{
1013+
for (int i = 0; i < 3; i++)
1014+
{
1015+
std::cout << "the buffer numbers of index "<< i << " is "<< buffer[i] << std:: endl;
1016+
}
1017+
}
1018+
)
1019+
1020+
$(P In a bar.d file: )
1021+
1022+
------
1023+
import std.stdio;
1024+
1025+
extern(C++) struct A
1026+
{
1027+
int a;
1028+
int b;
1029+
int[3] buffer;
1030+
this(int a, int b);
1031+
ref A opAssign(ref const A); // links with A::operator=
1032+
void opCall(int a, int b, int c); // links with A::operator()
1033+
ref int opIndex(int value); // links with A::operator[]
1034+
void printbuffer();
1035+
}
1036+
1037+
void main()
1038+
{
1039+
auto obj1 = A(1, 3);
1040+
auto obj2 = A(13, 43);
1041+
obj2 = obj1; // opAssign called here
1042+
writeln("obj2's a is ", obj2.a);
1043+
writeln("obj2's b is ", obj2.b);
1044+
obj2(20, 40, 60);
1045+
obj2[0] = 30;
1046+
obj2[1] = 60;
1047+
obj2[2] = 90;
1048+
obj2.printbuffer();
1049+
}
1050+
------
1051+
1052+
$(P Compiling, linking, and running produces the output:)
1053+
1054+
$(CONSOLE
1055+
$(GT) g++ -c foo.cpp
1056+
$(GT) dmd bar.d foo.o -L-lstdc++ && ./bar
1057+
obj2's a is 1
1058+
obj2's b is 3
1059+
value of a is 20
1060+
value of b is 40
1061+
value of c is 60
1062+
the buffer numbers of index 0 is 30
1063+
the buffer numbers of index 1 is 60
1064+
the buffer numbers of index 2 is 90
1065+
)
1066+
9611067
$(H2 $(LNAME2 rtti, Runtime Type Identification))
9621068

9631069
$(P D runtime type identification

0 commit comments

Comments
 (0)