By now most people know that ChatGPT can answer imprecise questions usefully and immediately. It provides answers with working code. Given a question posed independently can it come to a usable answer? I put it to the test.

We have a Telegram group of algorithmic traders. They’re a serious bunch: real HFT, working quants and the like. Grad students and recent grads trying to get into top firms.

Many of them code C++. The group has become one of the resources they use to get help with practical problems. A couple of days ago while waiting for the Eurostar to depart from Gare du Nord in Paris to London (where I’ll attend the C++ Meetup) a question came up on my phone:

“Hi guys, is there any way to automate the serialization of protobuf message? Like the following: C++ structs -> serialize to protobuf message struct -> send out to network”

I pulled up ChatGPT and posed it the question - cut and pasted.

Just as immediately as the answer always comes I had the reply:

#include <iostream>
#include <fstream>
#include <cxxreflect/cxxreflect.hpp>

// Define the struct
struct MyStruct {
    int a;
    float b;
    std::string c;
};

int main() {
    // Get the reflection information for the struct
    auto type = cxxreflect::get_type<MyStruct>();

    // Open the output file
    std::ofstream proto_file("MyStruct.proto");

    // Write the package name
    proto_file << "syntax = \"proto3\";\n\n";
    proto_file << "message MyStruct {\n";

    // Iterate over the fields of the struct
    for (auto field : type.fields()) {
        // Write the field type
        proto_file << "  " << field.type().name() << " ";
        // Write the field name
        proto_file << field.name() << " = ";
        // Write the field tag number
        proto_file << field.tag() << ";\n";
    }
    proto_file << "}\n";
    return 0;
}

We all thought this was very cool. Basically we had an answer to the question.

But, I thought “Actually, better to do it in Python, right?”

So I typed that exact phrase into ChatGPT. In the back of my mind I thought “he” (I happen to think of “him” as a guy) might instead give me a solution for a Python version of the initial question - since I provided no additonal context.

Nah, he got me. After all, he’s known me for a loooong time (in nanoseconds).

from cppheaderparser import CppHeader

header = CppHeader("MyStruct.h")
struct = header.classes["MyStruct"]

# Open the output file
with open("MyStruct.proto", "w") as proto_file:
    # Write the package name
    proto_file.write("syntax = \"proto3\";\n\n")
    proto_file.write("message MyStruct {\n")

    # Iterate over the fields of the struct
    for var in struct["variables"]:
        # Write the field type
        proto_file.write("  " + var["type"] + " ")
        # Write the field name
        proto_file.write(var["name"] + " = ")
        # Write the field tag number
        proto_file.write(str(var["index"]) + ";\n")
    proto_file.write("}\n")

And this is exactly what the questioner needed. His problem is solved by ChatGPT.

General Google searches could have reached the same result - but an order of magnitude more slowly at least.

Microsoft’s $10 billion to integrate ChatGPT into Word, Excel and Bing etc is a bargain.