首页 > 解决方案 > 太多组合来扩展宏

问题描述

我想将一个函数应用于数据缓冲区,并且它们的类型在运行时是已知的。我为此使用了一个模板函数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.

有没有我可以用来编译的解决方法?

谢谢

标签: c++boostc-preprocessor

解决方案


我不确定你想用 实现什么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您应该使用成员函数指针类型。


推荐阅读