From ddab4935516c3edd98e0daeabf123b8af540bc23 Mon Sep 17 00:00:00 2001 From: Mark Probst Date: Mon, 20 Jul 2026 16:58:48 -0400 Subject: [PATCH] fix(cpp): support std::optional in Utf16_Utf8 string conversion (#2383) Co-Authored-By: gpt-5.6-sol via pi --- .../language/CPlusPlus/CPlusPlusRenderer.ts | 22 +++++++++++++++++++ test/languages.ts | 4 +++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts b/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts index ec99e8029b..55e371d735 100644 --- a/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts +++ b/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts @@ -3403,6 +3403,28 @@ export class CPlusPlusRenderer extends ConvenienceRenderer { ); this.superThis.ensureBlankLine(); + if (this.superThis.haveOptionalProperties) { + this.superThis.emitLine( + "template", + ); + this.superThis.emitBlock( + [ + "static toType convert(tag<", + this.superThis._optionalType, + " >, tag<", + this.superThis._optionalType, + " >, fromType opt)", + ], + false, + () => { + this.superThis.emitLine( + "if (!opt) return toType(); else return toType(Utf16_Utf8::convert(*opt));", + ); + }, + ); + this.superThis.ensureBlankLine(); + } + this.superThis.emitLine("template"); this.superThis.emitBlock( [ diff --git a/test/languages.ts b/test/languages.ts index 6bcfd43611..95f7f17d3e 100644 --- a/test/languages.ts +++ b/test/languages.ts @@ -739,7 +739,9 @@ export const CPlusPlusLanguage: Language = { quickTestRendererOptions: [ { "source-style": "multi-source" }, { "code-format": "with-struct" }, - { wstring: "use-wstring" }, + // bug2521.json has an optional string, exercising UTF conversion + // through std::optional. + ["bug2521.json", { wstring: "use-wstring" }], { "const-style": "east-const" }, // The default is boost=false (C++17); this keeps the boost code // path covered. Pinned to specific inputs because the default