如何用宏转换声明?C++03下sc_fifo_in类型的数组改造方法
sc_fifo_in<matrix<...>> to Array of sc_fifo_in<data_type> in C++03 Great question—this is a classic pain point with C++03's limited metaprogramming and macro syntax constraints. Since macros can't parse nested angle brackets (<>) directly, we need workarounds that either split the type parameters explicitly or use template helpers to extract the necessary info. Here are a few solid approaches:
1. Template Metaprogramming Helper + Macro Wrapper
This approach uses a template to "decode" the matrix type's parameters, then a macro to generate the array declaration cleanly.
First, define a helper template that specializes for your sc_fifo_in<matrix<...>> type:
// Forward declaration of the helper template template <typename FifoType> struct FifoArrayDecoder; // Specialization for sc_fifo_in holding a matrix template <typename DataT, int WIDTH, int HEIGHT> struct FifoArrayDecoder<sc_fifo_in<matrix<DataT, WIDTH, HEIGHT> > > { // The target fifo type (sc_fifo_in with the inner data type) typedef sc_fifo_in<DataT> TargetFifo; // Total number of elements (width * height) static const int TotalElements = WIDTH * HEIGHT; };
Then create a macro that uses this helper to generate the array. Instead of trying to parse the existing sc_fifo_in<matrix<...>> declaration, we pass the raw parameters to the macro:
#define DECLARE_FIFO_ARRAY(var_name, DataT, WIDTH, HEIGHT) \ typename FifoArrayDecoder<sc_fifo_in<matrix<DataT, WIDTH, HEIGHT> > >::TargetFifo var_name[FifoArrayDecoder<sc_fifo_in<matrix<DataT, WIDTH, HEIGHT> > >::TotalElements]
Usage:
Instead of writing sc_fifo_in<matrix<data_type, WIDTH, HEIGHT> > in;, use:
DECLARE_FIFO_ARRAY(in, data_type, WIDTH, HEIGHT);
This expands exactly to sc_fifo_in<data_type> in[WIDTH * HEIGHT]; as needed.
2. Simplified Macro with Typedef (No Metaprogramming)
If you want to avoid template magic, you can use a macro that generates a temporary typedef (to avoid repeating the matrix type) and then declares the array. This is simpler but relies on unique variable names to avoid typedef name collisions:
#define MATRIX_FIFO_TO_ARRAY(var_name, DataT, WIDTH, HEIGHT) \ typedef matrix<DataT, WIDTH, HEIGHT> _tmp_matrix_##var_name; \ sc_fifo_in<DataT> var_name[WIDTH * HEIGHT]
Usage:
MATRIX_FIFO_TO_ARRAY(in, data_type, WIDTH, HEIGHT);
The temporary typedef (_tmp_matrix_in in this case) is harmless as long as your variable names are unique in the scope.
Why Direct Macro Replacement Isn't Possible
To be clear: you can't write a macro that takes sc_fifo_in<matrix<data_type, WIDTH, HEIGHT> > in; and directly replaces it with the array version. C++ preprocessor macros work on token streams, and angle brackets aren't part of valid macro identifiers. The preprocessor can't parse the nested <> structure to extract data_type, WIDTH, and HEIGHT automatically—you have to pass those parameters explicitly to the macro.
All the above solutions work within C++03's constraints, with the template-based approach being the most robust (no risk of name collisions, and it's extensible if you need to support other matrix-like types later).
内容的提问来源于stack exchange,提问作者ampawd

