基于结构中的字段数编译时间开关生成 [英] Compile time switch generation based on number of fields in structure
问题描述
在C ++ 03中如何在编译时获得所选结构的成员数?我正在试验BOOST_FUSION_ADAPT_STRUCT,但我没有得到任何工作示例。
编辑:
我想在编译时生成switch语句,每个成员一个病例。所以让我们说我们有3个成员的结构,然后我想生成这个开关:
switch(val)
{
case 0:
break;
case 1:
break;
case 2:
break;
}
我如何做这样的事情?
编辑2:
在每个语句中我将调用带有一些参数的模板函数。其中一个参数是结构成员。
编辑3:
经过长时间的搜索,阅读和查找本文 http://boost.2283326.n4.nabble.com/Preprocessor-sequence- of-quot-pairs-quot-td4636239.html
我设法迭代成员从0到count - 1(从创建switch语句很容易)。
#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 <<数字<< std :: endl;
int main()
{
BOOST_PP_REPEAT(BOOST_PP_SEQ_SIZE(SEQ1),PRINT,data)
}
现在BOOST_PP_REPEAT可能创建case语句...:)
解决方案解决方案...
经过长时间的搜索,阅读和查找本文 http://boost.2283326.n4.nabble.com/Preprocessor-sequence-of-quot-pairs-quot-td4636239 .html
我设法迭代成员从0到count - 1(从创建switch语句很容易)。
#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 <<数字<< std :: endl;
int main()
{
BOOST_PP_REPEAT(BOOST_PP_SEQ_SIZE(SEQ1),PRINT,data)
}
现在,BOOST_PP_REPEAT可能创建案例语句...:)
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.
EDIT: 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; }
How I can do something like this?
EDIT 2: in each statement I will call template function with some parameters. One of this parameters is a member of structure.
EDIT 3: After long searching, reading and finding this article http://boost.2283326.n4.nabble.com/Preprocessor-sequence-of-quot-pairs-quot-td4636239.html
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 ... :)
解决方案Solution...
After long searching, reading and finding this article http://boost.2283326.n4.nabble.com/Preprocessor-sequence-of-quot-pairs-quot-td4636239.html
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 ... :)
这篇关于基于结构中的字段数编译时间开关生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!