恢复定义和宏

时间:2017-06-08 17:12:26

标签: c c-preprocessor

有没有办法在C中恢复宏,这样你就可以定义一个新宏,在其名称下可能已经定义了其他宏,并用之前的值重新定义它?

这样当删除新定义的宏并最终重新定义的宏被重置为以前的状态时?

示例:

// a macro parameter used in a library
#define size 10
#include <library/use_size.h>

//here the command/pragma to save the definitions
    #define size (100 / sizeof(size_t))
    // some use of size ...
//here the command/pragma to reset the definitions

#include <library/allocator_with_size.h>

#undef size

// use size as a variable name
size_t size = 0;
//...
size += 123;

编辑:我不想使用#undef,因为它不会恢复旧的宏。此外,如果您有许多宏,例如在X-macro-list中使用它们(在长重复代码/常量数组和结构的声明中),如果有许多#undef指令,它看起来很难看。 / p>

1 个答案:

答案 0 :(得分:1)

好的,我研究了自己并找到了由clang,gcc和visual c ++支持的编译语var express = require('express'), bodyParser = require('body-parser'), http = require('http'), path = require('path'), cors = require('cors'); // Express server var app = module.exports = express(); app.set('port', process.env.PORT || 3000); // BrowserSync at development host if (app.get('env') === 'development') { var browserSync = require('browser-sync'), var bsconf = { host: 'localhost', port: 3001, https: false, notify: false, open: false, online: false, ui: false, server: { baseDir: path.join(__dirname, '/public') }, files: [path.join(__dirname, '/public/**/*.{html,css,js,json}')] }; var bs = browserSync.create().init(bsconf); app.use(require('connect-browser-sync')(bs)); } app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static(path.join(__dirname, 'public'))); app.use(cors()); // Route index.html app.get('/', function(req, res) { res.sendFile(path.join(__dirname, 'public', 'index.html')); }); // Starting express server http.createServer(app).listen(app.get('port'), function () { console.log('Listening on port ' + app.get('port') + '...'); }); push_macro。我使用clang,所以使用它没有问题。缺点:如果要恢复多个宏¹,它不会减少行数,但它会恢复宏并且可以封装:

pop_macro

备注:

¹我定义了多个宏并尝试使用以下命令恢复它们:

#pragma push_macro("size")
    #define size (100 / sizeof(size_t))
#pragma pop_macro("size")

但是这还没有在编译器中实现。因此,对于每个宏,必须有一个单独的行来执行此操作。