是否可以在一大块C++宏之间切换
Is it possible to flip between a large block of C++ macros?
假设我有一堆宏定义一些变量 - 它们需要在代码的不同部分以两种方式定义(是的,这是一个坏主意,但重构需要很长时间(。
是否有可能让下面的代码片段工作,即打印出 4 然后 1?
#include <iostream>
#define ENABLE
#ifdef ENABLE
#define B 4
#define C 5
//imagine a bunch more here
#else
#define B 1
#define C 2
//imagine a bunch more here
#endif
int main()
{
std::cout << B << std::endl;
#pragma push_macro("ENABLE")
#undef ENABLE
std::cout << B << std::endl;
#pragma pop_macro("ENABLE")
return 0;
}
通过专门定义 B 当然可以实现相同的效果,但是如果我有一大块宏,那就不是 100% 实用的:
#include <iostream>
#define B 4
int main()
{
#pragma push_macro("B")
#undef B
#define B 1
std::cout << B << std::endl;
#pragma pop_macro("B")
std::cout << B << std::endl;
return 0;
}
您可以通过
重复#include
头文件来做到这一点:
页眉:
// Note: no inclusion guards!
#undef B
#undef C
#ifdef ENABLE
#define B 4
#define C 5
//imagine a bunch more here
#else
#define B 1
#define C 2
//imagine a bunch more here
#endif
src 文件中的用法:
int main()
{
#define ENABLE
#include "header"
std::cout << B << std::endl;
#undef ENABLE
#include "header"
std::cout << B << std::endl;
return 0;
}
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 构造函数和转换运算符之间的重载解析
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数