* [C++] Member pragma RFC #530
rebase devel

* changes the test so `echo` is not used before Nim is init

* rebase devel

* fixes Error: use explicit initialization of X for clarity [Uninit]
This commit is contained in:
Juan M Gómez
2023-08-07 09:11:00 +01:00
committed by GitHub
parent fe9ae2c69a
commit b5b4b48c94
7 changed files with 85 additions and 23 deletions

53
tests/cpp/tmember.nim Normal file
View File

@@ -0,0 +1,53 @@
discard """
targets: "cpp"
cmd: "nim cpp $file"
output: '''
2
false
hello foo
hello boo
hello boo
destructing
destructing
'''
"""
proc print(s: cstring) {.importcpp:"printf(@)", header:"<stdio.h>".}
type
Doo {.exportc.} = object
test: int
proc memberProc(f: Doo) {.exportc, member.} =
echo $f.test
proc destructor(f: Doo) {.member: "~'1()", used.} =
print "destructing\n"
proc `==`(self, other: Doo): bool {.member:"operator==('2 const & #2) const -> '0"} =
self.test == other.test
let doo = Doo(test: 2)
doo.memberProc()
echo doo == Doo(test: 1)
#virtual
proc newCpp*[T](): ptr T {.importcpp:"new '*0()".}
type
Foo = object of RootObj
FooPtr = ptr Foo
Boo = object of Foo
BooPtr = ptr Boo
proc salute(self: FooPtr) {.member: "virtual $1()".} =
echo "hello foo"
proc salute(self: BooPtr) {.member: "virtual $1()".} =
echo "hello boo"
let foo = newCpp[Foo]()
let boo = newCpp[Boo]()
let booAsFoo = cast[FooPtr](newCpp[Boo]())
foo.salute()
boo.salute()
booAsFoo.salute()