c++ - 太多组合来扩展宏
问题描述
我想将一个函数应用于数据缓冲区,并且它们的类型在运行时是已知的。我为此使用了一个模板函数template <typename T1, typename T2, typename T3> void myFunction()
。
myFunction
是一个类的成员,该类还包含存储缓冲区的数据结构。缓冲区存储在 char* 指针中,我有一个枚举来了解缓冲区的实际数据类型,从而允许我将指针转换为正确的类型。我还有一个数据结构来以这种方式注册所有数据类型组合:
// functionPtr is declared earlier
functionPtr = static_cast<void(*)(void)>( &myFunction< DataType1, DataType2, DataType3 >);
registerFunction(functionPtr);
最后,我写了一个宏来解析每种类型的组合。
我的问题是,对于编译器来说,要扩展的数据似乎太多了。我简化为下面的最小示例:
预处理, 915 字节
#include <boost/preprocessor.hpp>
// List the possible types
#define STRIP_ALL_TYPES \
(eIBT_Int8)(eIBT_UInt8) \
(eIBT_Int16)(eIBT_UInt16) \
(eIBT_Int32)(eIBT_UInt32) \
(eIBT_Real32) \
(eIBT_Binary) \
(eIBT_Label16)(eIBT_Label32)
# Generate all the combinations
#define GENERATE_TYPE_COMBINATION(r, product) (product)
// Generate the instruction for a given type combination
#define TEMPLATE_SPECIFIC_TYPE_COMBINATION(r, data, elem)\
functionPtr = static_cast<void(*)(void)>(\
&CurProcessorType::myFunction< BOOST_PP_SEQ_ENUM(elem) >);
// Generate all the possible instructions
#define GENERATE_TEMPLATE(ST)\
BOOST_PP_SEQ_FOR_EACH(TEMPLATE_SPECIFIC_TYPE_COMBINATION, _, \
BOOST_PP_SEQ_FOR_EACH_PRODUCT(GENERATE_TYPE_COMBINATION, ST))
GENERATE_TEMPLATE((STRIP_ALL_TYPES)(STRIP_ALL_TYPES)(STRIP_ALL_TYPES))
通过扩展宏,编译器编译生成的行应如下所示:
functionPtr = static_cast<void(*)(void)>( &CurProcessorType::myFunction< eIBT_Label16, eIBT_Label16, eIBT_Label16 >);
但是当我在 TIO 上测试我的代码时,我得到了错误.code.tio:23:1: error: macro "BOOST_PP_IIF_1" requires 2 arguments, but only 1 given GENERATE_TEMPLATE((STRIP_ALL_TYPES)(STRIP_ALL_TYPES)(STRIP_ALL_TYPES))
它仅适用于STRIP_ALL_TYPES
.
有没有我可以用来编译的解决方法?
谢谢
解决方案
我不确定你想用 实现什么BOOST_PP_SEQ_FOR_EACH
,因为BOOST_PP_SEQ_FOR_EACH_PRODUCT
已经为你提供了模板参数的所有排列:
#include <boost/preprocessor.hpp>
// List the possible types
#define STRIP_ALL_TYPES \
(eIBT_Int8)(eIBT_UInt8) \
(eIBT_Int16)(eIBT_UInt16) \
(eIBT_Int32)(eIBT_UInt32) \
(eIBT_Real32) \
(eIBT_Binary) \
(eIBT_Label16)(eIBT_Label32)
// Generate all the combinations
#define GENERATE_TYPE_COMBINATION(r, product)\
functionPtr = static_cast<void(*)(void)>(\
&CurProcessorType::myFunction< BOOST_PP_SEQ_ENUM(product) >);
#define GENERATE_TEMPLATE(ST)\
BOOST_PP_SEQ_FOR_EACH_PRODUCT(GENERATE_TYPE_COMBINATION, ST)
GENERATE_TEMPLATE((STRIP_ALL_TYPES)(STRIP_ALL_TYPES)(STRIP_ALL_TYPES))
另外,我应该注意,如果是非静态成员函数,则转换void(*)(void)
为无效。CurProcessorType::myFunction
您应该使用成员函数指针类型。
推荐阅读
- javascript - 禁用键盘快捷键
- c# - 如何在 Xamarin Android 中一次从图库中选择多个图像(复选框)?
- dialogflow-es - 可以修改 Dialogflow 响应时间限制吗?
- excel - 复制并粘贴带有变量名的 excel 文件
- arrays - 打印数组值的不同结果
- git - 如何将更改的 Git 存储库与我的本地合并?
- node.js - 试图理解“由于 Electron 很可能使用不同的 V8 版本”解释
- javascript - 基于页面高度的css三角形
- inspeqtor - 循环是什么意思?
- python - 如何使用我自己的 Python 机器人在 Discord 服务器上获得加入职位?