Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of elements in a struct?

Tags:

c++

I declared a struct for some options that shall be filled by command line arguments or by reading an input file:

struct options {
  int val1;
  int val2;
  bool val3;
}

Now I want to check for the correct number of arguments at execution of the program. Sure thing a

const int optionsSize = 3;

would do. But is there any adaptive way? What if I add another value into the struct and don't remember to increase the integer?

like image 512
Gunnar Avatar asked Oct 26 '10 14:10

Gunnar


1 Answers

Why not add the options as specified into an std::vector<string> options and use the options.size() method to check the correct number. Then convert them to the proper datatype.

A more robust way of doing this kind of thing would be to use Boost Program Options

like image 168
RC. Avatar answered Sep 19 '22 12:09

RC.