diff --git a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala index 094ea3e23..50b3d4d9c 100644 --- a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala +++ b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala @@ -854,7 +854,7 @@ class TranslatorSpec extends AnyFunSpec { describe("to do type enforcement") { // type enforcement: casting to non-literal byte array full("[0 + 1, 5].as", CalcIntType, CalcBytesType, ResultMap( - CppCompiler -> "???", + CppCompiler -> "std::string({char(0 + 1), char(5)})", CSharpCompiler -> "new byte[] { 0 + 1, 5 }", GoCompiler -> "[]uint8{0 + 1, 5}", JavaCompiler -> "new byte[] { 0 + 1, 5 }", diff --git a/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala index b5686af2a..cabc8858e 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/CppTranslator.scala @@ -111,12 +111,22 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import // TODO: C++14 } } else { - throw new RuntimeException("C++ literal arrays are not implemented yet") + throw new RuntimeException("C++ literal arrays are not implemented yet without list initializers") } } override def doByteArrayLiteral(arr: Seq[Byte]): String = "std::string(\"" + Utils.hexEscapeByteArray(arr) + "\", " + arr.length + ")" + override def doByteArrayNonLiteral(values: Seq[Ast.expr]): String = { + // It is assumed that every expression produces integer in the range [0; 255] + if (config.cppConfig.useListInitializers) { + "std::string({" + values.map(value => s"static_cast(${translate(value)})").mkString(", ") + "})" + } else { + // TODO: We need to produce an expression, but this is possible only with initializer lists + // or variadic templates (if use a helper function) which both available only since C++11 + throw new RuntimeException("C++ non-literal arrays are not implemented yet without list initializers") + } + } override def genericBinOp(left: Ast.expr, op: Ast.operator, right: Ast.expr, extPrec: Int) = { (detectType(left), detectType(right), op) match {