How in C++03 get in compile time number of members of chosen struct? I was experimenting with BOOST_FUSION_ADAPT_STRUCT
but I did't get any working example.
I want to generate switch statement in compile time, where there will be one case per each member. So lets say we have struct with 3 members then I want to generate this switch:
switch(val)
{
case 0:
break;
case 1:
break;
case 2:
break;
}
In each statement I will call template function with some parameters. One of this parameters is a member of structure.
How I can do something like this?
After long searching, reading and finding this article. I managed to iterate over members from 0 to count - 1 (from that creating switch statement is easy).
#include <iostream>
#include <string>
#include <vector>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/fusion/include/define_struct.hpp>
#include <boost/preprocessor/seq/size.hpp>
#include <boost/preprocessor/seq/seq.hpp>
#include <boost/preprocessor/seq/cat.hpp>
struct MyStruct
{
int x;
int y;
};
#define GO(r, data, elem) elem
#define SEQ1 ((int,x))((int,y))
BOOST_FUSION_ADAPT_STRUCT(
MyStruct,
BOOST_PP_SEQ_FOR_EACH(GO, ,SEQ1)
)
#define PRINT(unused, number, data) \
std::cout << number << std::endl;
int main()
{
BOOST_PP_REPEAT(BOOST_PP_SEQ_SIZE(SEQ1), PRINT, "data")
}
Now BOOST_PP_REPEAT
may create case statements.