Best way to encapsulate "optional" fields inside a structure in general in C ++?

I have many specific structures, and I want to designate the fields as optional (real or not). It's just interesting what ideas people have to achieve this. Here is an example struct (fields can be other structures, even structure vectors):

 struct LogonMessage_t
       {
          Header_t header; // this points to another struct containing all primitives
          std::string username;
          std::string password;
          std::vector<LogonOption_t> LogonOptions;
          int subaccountid;
          std::string Text;
       }

I would like that by default all fields are not displayed and include them one at a time, possibly in their setters. Since these structures are generated, a general method is preferred.

My ideas so far:

  • A bitmap of fields indicating whether the field is set or not
  • Use sentinel values ​​("\ 0" for std :: string, -1 for int, -1.0f for float
  • - /- , , , , - ? , .

Btw, STL , , .

+3
4
+6

. - , - .

, ( ).

+1

, , if (username.empty()) ..

a subaccountid , 0 . -1 .

0

. : .

std:: pair, - . - boost:: shard_ptr, 0, .

Value, . const & value() const {return} & value() {return}

0

All Articles