【问题标题】:How to transform one declaration into another with a tricky macro如何使用棘手的宏将一个声明转换为另一个声明
【发布时间】:2018-02-26 20:19:34
【问题描述】:

考虑这个声明:

sc_fifo_in<matrix<data_type, WIDTH, HEIGHT> > in;

我想要的是一些类似函数的宏或它们的组合,它将上面的声明替换为:

sc_fifo_in<data_type> in[WIDTH * HEIGHT];

我的观察是<>符号不能被宏替换,因为宏标识符的名称不能包含这样的字符。

那么还有其他的解决方法吗?

如何用 C++03 实现这一点???

【问题讨论】:

  • 编写一个程序来读取文件并为您执行替换将非常简单。
  • @NathanOliver 当然解析将是一个最简单的解决方案,但这里的重点是在不读取文件、解析文件等的情况下做到这一点
  • 然后使用typedef
  • @Raindrop7 怎么样?
  • 我只会使用我的编辑器 (Sublime) 跨文件全局替换它。

标签: c++ macros c-preprocessor


【解决方案1】:

如果你有 vim 可用,你可以将以下命令保存在一个文件中。

1,$s/sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge
wa
q

我们将文件命名为 temp.vim。

你可以执行:

vim -S temp.vim <filename>

更新文件。

s命令解释:

  1,$s/sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge

  1,$s -> substitue in all the lines of the file

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge

       ^  ----    The pattern to subtitute -------                         ^
                                                                             ^ -- substitution   --   ^

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge

                 ^     ^ -> Ignore everthing up to and including the second < after sc_fifo_in.

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge

                        ^       ^ -> Capture everything up to the next , (data_type)

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge


                                     ^       ^ -> Capture everything up to the next , (WIDTH)

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge


                                                  ^       ^ -> Capture everything up to the next > (HEIGHT)

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge


                                                                   ^       ^ -> Capture everything up to ; (in)

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge

                                                                                        ^ The data type (the first capture)

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge

                                                                                            ^ The variable name (the fourth capture)

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge

                                                                                               ^ WIDTH (the second capture)

       sc_fifo_in<[^<]*<\([^,]*\),\s*\([^,]*\),\s*\([^>]*\)>\s*>\s\([^;]*\);/sc_fifo_in<\1> \4[\2 * \3];/ge

                                                                                                    ^ HEIGHT (the third capture)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多