Search code examples
c++thrift

How do I set fields in Thrift struct properly in C++ sources?


I notice there is a method named __set_FIELD in the generated C++ class MyThriftStruct of my thrift structure MyThriftStruct. Calling this method will set the attribute MyThriftStruct.__is_set.FIELD to true which tells the structure this field has been set.

I also notice that in the official tutorial they set the filed of thrift structure by directly copying the value:

MyThriftStruct.FIELD = val;

The first method seems ugly but the latter one didn't change the __isset flag. So which one should I use to set the field of thrift struct in C++?


Solution

  • it depends: for required fields or default (neither required nor optional) fields MyThriftStruct.FIELD = val; is enough.

    set_FIELD is required only for optional fields (or manual modification of __isset struct fields is required to ensure optional field serialization)