diff options
| author | Tyge Løvset <[email protected]> | 2021-05-18 22:30:26 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-05-18 22:30:26 +0200 |
| commit | 037090beddb7eb02f5527681cb2cafc18a087264 (patch) | |
| tree | 4fa70c62e1571414479e6c03d07f9967fbaa6499 | |
| parent | 353ed880f92403d29c32af8d36c4bb8b0cb9c821 (diff) | |
| download | STC-modified-037090beddb7eb02f5527681cb2cafc18a087264.tar.gz STC-modified-037090beddb7eb02f5527681cb2cafc18a087264.zip | |
Updated cvec_str example.
| -rw-r--r-- | README.md | 21 |
1 files changed, 10 insertions, 11 deletions
@@ -215,17 +215,16 @@ and non-emplace methods: using_cvec_str(); // vector of string (cstr)
...
cvec_str vec = cvec_str_init();
-cstr s = cstr_from("a new string");
-
-cvec_str_push_back(&vec, cstr_from("Hello")); // construct and add string
-cvec_str_push_back(&vec, cstr_clone(s)); // clone and add an existing string
-
-cvec_str_emplace_back(&vec, "Yay, literal"); // internally constructs cstr from string-literal
-cvec_str_emplace_back(&vec, cstr_clone(s)); // <-- COMPILE ERROR: wrong input type
-cvec_str_emplace_back(&vec, s.str); // Ok: const char* input type (= rawvalue).
-
-cstr_del(&s);
-cvec_del(&vec);
+cstr s = cstr_new("a string literal"); // cstr_new() for literals; no strlen() usage
+c_defer (cvec_str_del(&vec), cstr_del(&s)) // defer the destructors to end of block:
+{
+ cvec_str_push_back(&vec, cstr_from("Hello")); // construct and add string from const char*
+ cvec_str_push_back(&vec, cstr_clone(s)); // clone and add an existing string
+
+ cvec_str_emplace_back(&vec, "Yay, literal"); // internally constructs cstr from string-literal
+ cvec_str_emplace_back(&vec, cstr_clone(s)); // <-- COMPILE ERROR: wrong input type
+ cvec_str_emplace_back(&vec, s.str); // Ok: const char* input type (= rawvalue).
+}
```
This is made possible because the **using**-declarations may be given an optional
conversion/"rawvalue"-type as template parameter, along with a back and forth conversion
|
