nim-cppstl icon indicating copy to clipboard operation
nim-cppstl copied to clipboard

doesn't work with destructors

Open timotheecour opened this issue 3 years ago • 1 comments

continuing of https://github.com/kaushalmodi/std_vector/issues/4 since the other repo was merged here

example

when defined case2:
  import cppstl/std_vector
  type Foo = object
    x: int
  proc `=destroy`*(a: var Foo) {.inline.} =
    echo ("in destroy", a.x)
  proc main =
    var v1 = initCppVector[Foo]()
    echo v1
    var v = initCppVector[Foo]()
    v.add Foo(x: 10)
    v.add Foo(x: 11)
    v.add Foo(x: 12)
    echo "ok0"
    echo v
    echo "ok1"
    echo v
    echo "ok2"
  main()

gives:

[]
ok0
("in destroy", 10)
("in destroy", 11)
("in destroy", 12)
[(x: 10), (x: 11), (x: 12)]
ok1
("in destroy", 10)
("in destroy", 11)
("in destroy", 12)
[(x: 10), (x: 11), (x: 12)]
ok2

(with both gc:arc|refc)

expected result

same as for here, with seq instead of vector:

when defined case3:
  import cppstl/std_vector
  type Foo = object
    x: int
  proc `=destroy`*(a: var Foo) {.inline.} =
    echo ("in destroy", a.x)
  proc main =
    var v: seq[Foo]
    v.add Foo(x: 10)
    v.add Foo(x: 11)
    v.add Foo(x: 12)
    echo "ok0"
    echo v
    echo "ok1"
    echo v
    echo "ok2"
  main()

with --gc:arc and --gc:refc, this gives:

ok0
@[(x: 10), (x: 11), (x: 12)]
ok1
@[(x: 10), (x: 11), (x: 12)]
ok2
("in destroy", 10)
("in destroy", 11)
("in destroy", 12)

as it should

timotheecour avatar Jun 28 '21 03:06 timotheecour