Is it possible to get the field counts of a message at compile time

141 views
Skip to first unread message

Yuhui ZHI

unread,
May 7, 2022, 2:27:35 AM5/7/22
to Protocol Buffers
I'm working with Protobuf3 in my C++14 project.

Let's say that I have such a message in some proto file:

message User {
    uint32 user_id = 1;
    string user_name = 2;
    map<string, string> others = 10;
};

Now, I'm trying to use the reflection mechanism in some functions to process this message.

I need to get the field counts of the message User, which should be 3.

The doc told me that there was a function field_count(), which can give me the 3.

However, it seems that this is a common runtime function. I could work with it but I want to make come compile warning/error with static_assert. So I need this 3 at compile time.

Is it possible to get the field counts of a message at compile time?

Deanna Garcia

unread,
Aug 1, 2022, 5:27:48 PM8/1/22
to Protocol Buffers
Unfortunately we don't have tools to access field counts at compile time. In order to do this we would have to increase the size of our generated files which is too big of a tradeoff for us to make.
Reply all
Reply to author
Forward
0 new messages