Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,22 @@ proc getTempCpp(p: BProc, t: PType, value: Rope): TLoc =
inc(p.labels)
result = TLoc(snippet: "T" & rope(p.labels) & "_", k: locTemp, lode: lodeTyp t,
storage: OnStack, flags: {})
# `auto` and `decltype(auto)` works differently in C++
#[
int& foo(int& x) {
return x;
}

int main()
{
int x = 100;
auto y = foo(x); // y is int
decltype(auto) z = foo(x); // z is int&
}
]#
p.s(cpsStmts).addVar(kind = Local,
name = result.snippet,
typ = "auto",
typ = "decltype(auto)",
initializer = value)

proc getIntTemp(p: BProc): TLoc =
Expand Down
5 changes: 0 additions & 5 deletions tests/misc/tsizeof.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ macros api OK
'''
"""

# This is for Azure. The keyword ``alignof`` only exists in ``c++11``
# and newer. On Azure gcc does not default to c++11 yet.
when defined(cpp) and not defined(windows):
{.passC: "-std=c++11".}

# Object offsets are different for inheritance objects when compiling
# to c++.

Expand Down
55 changes: 55 additions & 0 deletions tests/proc/tprocvar_var_ret.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
discard """
targets: "c cpp"
"""

proc testVarRet(x: var int): var int = x

proc testProcTypeParam(prc: proc (x: var int): var int {.nimcall.}) =
var x = 123
prc(x) = 321
doAssert x == 321
var y = 456
prc(x) = prc(y)
doAssert x == 456 and y == 456

testProcTypeParam(testVarRet)

var pVarRet = testVarRet
var testVar = 1234
pVarRet(testVar) = 5678
doAssert testVar == 5678

proc testVarRetStr(x: var string): var string = x

proc testProcTypeParamStr(prc: proc (x: var string): var string {.nimcall.}) =
var x = "foo"
prc(x) = "bar"
doAssert x == "bar"
var y = "baz"
prc(x) = prc(y)
doAssert x == "baz" and y == "baz"

testProcTypeParamStr(testVarRetStr)

var pVarRetStr = testVarRetStr
var testVarStr = "abc"
pVarRetStr(testVarStr) = "def"
doAssert testVarStr == "def"

type
FooObj = object
x, y: int

proc testVarRetObj(x: var FooObj): var FooObj = x

proc testProcTypeParamObj(prc: proc (x: var FooObj): var FooObj {.nimcall.}) =
var x = FooObj(x: 111, y: 222)
prc(x) = FooObj(x: 333, y: 444)
doAssert x == FooObj(x: 333, y: 444)

testProcTypeParamObj(testVarRetObj)

var pVarRetObj = testVarRetObj
var testVarObj = FooObj(x: 10, y: 20)
pVarRetObj(testVarObj) = FooObj(x: 30, y: 40)
doAssert testVarObj == FooObj(x: 30, y: 40)
Loading