Skip to content

Commit 19d58d8

Browse files
committed
cpp atomic good example
1 parent c44a791 commit 19d58d8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

compiler/semstmts.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,8 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
716716
let actualType = v.typ.skipTypes({tyGenericInst, tyAlias,
717717
tyUserTypeClassInst})
718718
if actualType.kind in {tyObject, tyDistinct} and
719-
actualType.requiresInit:
719+
actualType.requiresInit and not (sfImportc in actualType.sym.flags):
720+
# imported type use requiresInit pragma prevent implicit initialization
720721
defaultConstructionError(c, v.typ, v.info)
721722
else:
722723
checkNilable(c, v)

tests/cpp/tatomic.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
discard """
2+
targets: "cpp"
3+
action: compile
4+
"""
5+
import atomics
6+
7+
template relaxed*[T](location: var Atomic[T], value: T) =
8+
## use template avoid copy Atomic[T] to temporary variable
9+
location.store(value, moRelaxed)
10+
11+
var atom: Atomic[int]
12+
atom.relaxed(1)
13+
atom.relaxed(2)

0 commit comments

Comments
 (0)