summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJuan M Gómez <info@jmgomez.me>2023-09-15 11:08:41 +0100
committerGitHub <noreply@github.com>2023-09-15 12:08:41 +0200
commitcd0d0ca5304528e33eb10e87ea27936f38bfea1e (patch)
treec0772e95b8f79db6818f0fec2f2b7ce1c04ce651
parentae0a3f65c6a71c2fe50ac64549f1a6827949744d (diff)
downloadNim-cd0d0ca5304528e33eb10e87ea27936f38bfea1e.tar.gz
Document C++ Initializers (#22704)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
-rw-r--r--doc/manual_experimental.md27
1 files changed, 27 insertions, 0 deletions
diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md
index 72e883cbc..dd28fa67c 100644
--- a/doc/manual_experimental.md
+++ b/doc/manual_experimental.md
@@ -2382,6 +2382,33 @@ In the example above `CppClass` has a deleted default constructor. Notice how by
 
 Notice when calling a constructor in the section of a global variable initialization, it will be called before `NimMain` meaning Nim is not fully initialized.
 
+Constructor Initializer
+=======================
+
+By default Nim initializes `importcpp` types with `{}`. This can be problematic when importing
+types with a deleted default constructor. In order to avoid this, one can specify default values for a constructor by specifying default values for the proc params in the `constructor` proc.
+
+For example:
+
+```nim
+
+{.emit: """/*TYPESECTION*/
+struct CppStruct {
+  CppStruct(int x, char* y): x(x), y(y){}
+  int x;
+  char* y;
+};
+""".}
+type
+  CppStruct {.importcpp, inheritable.} = object
+
+proc makeCppStruct(a: cint = 5, b:cstring = "hello"): CppStruct {.importcpp: "CppStruct(@)", constructor.}
+
+(proc (s: CppStruct) = echo "hello")(makeCppStruct()) 
+# If one removes a default value from the constructor and passes it to the call explicitly, the C++ compiler will complain.
+
+```
+
 Member pragma
 =============