宏这是最初发布的回答为c++ macros with memory?C++与记忆
但不知何故,我不能够得到这个编译。我可能会在这里错过一些东西。 (我有一种感觉,这是一件C++可以做)
的main.cpp
#include <iostream>
using namespace std;
const char * hello = "hello";
const char * world = "world";
#define VAR
#define MEMORIZE world
#include "memorize.h"
#define MEMORIZE hello
#include "memorize.h"
int main() {
cout << VAR << endl;
return 0;
}
memorize.h
#undef VAR
#ifndef MEMORIZE
# error "No Argument to memorize.h"
#endif
#define VAR MEMORIZE
#undef MEMORIZE
,我得到的编译错误是这样的:
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
error: use of undeclared identifier 'MEMORIZE'
cout << VAR << endl;
^
note: instantiated from:
#define VAR MEMORIZE
^
1 error generated.
make[2]: *** [CMakeFiles/main.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
我真的想让这个记忆的东西在预处理阶段工作。有人可以帮忙吗?我认为BOOST_PP_COUNTER
在1.49
也使用这种技术,但我无法弄清楚如何。
我希望这是一种自虐的娱乐,而不是生产代码。 :-D – stinky472 2012-03-02 00:06:04
好吧,目前它不编译... 即使我想把它放在更广泛的用途,我不能:P – 2012-03-02 00:11:17