C++ 使用boost防止意外的宏重新扩展?
这里的目标是简单地得到C++ 使用boost防止意外的宏重新扩展?,c++,boost,C++,Boost,这里的目标是简单地得到a,b,c而不是它们的实际值。设置“足够简单”: 我认为自己非常聪明,已经在一些显式字符串中对值进行了排序,现在我定义了“真实”代码所需的实际值 // Now that we have the "final" values, actually define the real values // in real code, it's some lengthy nested namespaces (inconvenient to type) #define a 123 #def
a
,b
,c
而不是它们的实际值。设置“足够简单”:
我认为自己非常聪明,已经在一些显式字符串中对值进行了排序,现在我定义了“真实”代码所需的实际值
// Now that we have the "final" values, actually define the real values
// in real code, it's some lengthy nested namespaces (inconvenient to type)
#define a 123
#define b 456
#define c 789
最后,让我们打印它们以确保它们不会被扩展:
// Let there be printing!
#define GOTTA_PRINT_EM_ALL(r,data,i,elem) << ((i)+1) << ". " << elem << std::endl
int main(int argc, const char **argv) {
std::cout << "Humans: " << std::endl
BOOST_PP_SEQ_FOR_EACH_I(GOTTA_PRINT_EM_ALL,,SEQ_HUMAN);
}
假设它们应该是
std::string(“a”)
…那么真正的值是如何回到那里的呢?!我想可能是来自std::string
构造函数的(“a”)
造成了问题,但似乎不是这样(BOOST\u PP\u STRINGIZE
导致了相同的行为)。有什么建议吗?宏确实扩展为代码标记:
现在,当您将代码标记插入到gotton\u PRINT\u EM\u ALL
宏中时,您将得到
<< ((0)+1) << ". " << std::string(\"123\") << std::endl << ((1)+1) << ". " << std::string(\"456\") << std::endl << ((2)+1) << ". << std::string(\"789\")" << std::endl
要获得“代码令牌”,您还需要将其串接:
// Let there be printing!
#define GOTTA_PRINT_EM_ALL(r,data,i,elem) << ((i)+1) << ". " << BOOST_PP_STRINGIZE(elem) << std::endl
查看它
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <iostream>
#include <string>
#define a 123
#define b 456
#define c 789
#define SEQ (a)(b)(c)
// Try to create "final" value with `std::string("elem")`
// Brought in for explicit `std::string`, but no dice
#define MAKE_STRING(x) std::string(#x)
#define MAKE_XSTRING(x) MAKE_STRING(x)
#define HUMANIZE(r, data, elem) (MAKE_XSTRING(elem))
#define SEQ_HUMAN BOOST_PP_SEQ_FOR_EACH(HUMANIZE,,SEQ)
// Let there be printing!
#define GOTTA_PRINT_EM_ALL(r,data,i,elem) << ((i)+1) << ". " << BOOST_PP_STRINGIZE(elem) << std::endl
int main() {
std::cout << "Humans: " << std::endl
BOOST_PP_SEQ_FOR_EACH_I(GOTTA_PRINT_EM_ALL,,SEQ_HUMAN);
}
#包括
#包括
#包括
#包括
#包括
#定义一个123
#定义B456
#定义C789
#定义如下(a)(b)(c)
//尝试使用'std::string(“elem”)创建“final”值`
//为显式'std::string'引入,但没有骰子
#定义MAKE_STRING(x)std::STRING(#x)
#定义MAKE_XSTRING(x)MAKE_STRING(x)
#定义人性化(r,数据,元素)(makexstring(元素))
#定义每个人的顺序(人性化,顺序)
//让我们来印刷吧!
#我现在意识到这不是你想问的。我会把这个“答案”留在这里,这样你就可以澄清这个问题了。我现在明白了,但一点也不清楚。创建一个独立的示例程序,不留下任何解释。使用链接重复问题的答案:Hi@sehe感谢您的解释,并将我链接到我找不到的问题。这一切现在变得更有意义了,我想我并没有真正意识到引号在使用之前是转义的
test.cpp|24 col 1| note: #pragma message: Humans: (std::string("123")) (std::string("456")) (std::string("789"))
<< ((0)+1) << ". " << std::string(\"123\") << std::endl << ((1)+1) << ". " << std::string(\"456\") << std::endl << ((2)+1) << ". << std::string(\"789\")" << std::endl
Humans:
1. 123
2. 456
3. 789
// Let there be printing!
#define GOTTA_PRINT_EM_ALL(r,data,i,elem) << ((i)+1) << ". " << BOOST_PP_STRINGIZE(elem) << std::endl
Humans:
1. std::string("123")
2. std::string("456")
3. std::string("789")
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <iostream>
#include <string>
#define a 123
#define b 456
#define c 789
#define SEQ (a)(b)(c)
// Try to create "final" value with `std::string("elem")`
// Brought in for explicit `std::string`, but no dice
#define MAKE_STRING(x) std::string(#x)
#define MAKE_XSTRING(x) MAKE_STRING(x)
#define HUMANIZE(r, data, elem) (MAKE_XSTRING(elem))
#define SEQ_HUMAN BOOST_PP_SEQ_FOR_EACH(HUMANIZE,,SEQ)
// Let there be printing!
#define GOTTA_PRINT_EM_ALL(r,data,i,elem) << ((i)+1) << ". " << BOOST_PP_STRINGIZE(elem) << std::endl
int main() {
std::cout << "Humans: " << std::endl
BOOST_PP_SEQ_FOR_EACH_I(GOTTA_PRINT_EM_ALL,,SEQ_HUMAN);
}