编写宏以字符串化结构的内容
Write a Macro to stringify the contents of a struct
我正在尝试编写一个宏来扩展结构的内容:
struct Str
{
int a;
float f;
char *c;
};
Str s = {123, 456.789f, "AString"};
#define STRINGIFY_STR(x) ... // Macro to stringify x which is an instance of Str
printf("%s", STRINGIFY_STR(s));
期望输出: [a: 123, 传真:456.789, c:AString]
是否可以编写一个执行此操作的宏?如果是,那又如何?
有什么理由想把它作为一个宏来做吗?
您应该编写一个函数来执行此操作,而不是使用预处理器。
根据您的目标,有几种选择。 提升格式库提供了一个很好的工具包来构建格式化字符串。 您也可以始终重载operator<<
以提供干净的输出。
如果你在纯 C 中执行此操作,sprintf 系列方法可用于创建格式化输出。
这很恶心,仅适用于 gcc
/g++
.
#define STRINGIFY_STR(x)
std::string(({ std::ostringstream ss;
ss << "[a: " << x.a
<< ", f: " << x.f
<< ", c: " << x.c << "]";
ss.str(); })).c_str()
您必须从值创建字符串。请不要这样做。请听从里德的建议。
以下是我将如何修改您的struct
以使其可以打印出来:
struct Str
{
int a;
float f;
char *c;
std::ostream & dump (std::ostream &os) const {
return os << "[a: " << a
<< ", f: " << f
<< ", c: " << c << "]";
}
};
std::ostream & operator << (std::ostream &os, const Str &s) {
return s.dump(os);
}
Str s = {123, 456.789f, "AString"};
现在,要打印出s
,您可以使用std::cout
:
std::cout << s << std::endl;
或者,如果你真的想要一个字符串:
std::stringstream ss;
s.dump(ss);
puts(ss.str().c_str());
struct stringify
{
operator std::string() const
{
return str ;
}
std::string str ;
};
template < typename T > stringify operator+ ( stringify s, const T& object )
{
std::ostringstream stm ;
stm << object ;
if( !s.str.empty() && !std::isspace( *s.str.rbegin() ) ) s.str += ' ' ;
s.str += stm.str() ;
return s ;
}
我也反对这种方法,但要回答这个问题:
#define STRINGIFY_STR(x)
(std::string("[a: ") + std::to_string((long double)x.a)
+ std::string(", f:") + std::to_string((long double)x.f)
+ std::string(",c: ") + x.c + std::string("]") ).c_str()
我建议编写一个成员函数来执行此操作。
相关文章:
- C++结构到字符串
- 从字符串中获取结构的大小
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 如何解析表示树状数据结构的字符串
- 如何将字符串与结构(也包括字符串)进行比较?
- 如果结构中的字符串比使用的 p/调用签名长或短,会发生什么情况?
- 在向量 C++14(无限制联合)的结构内的联合中创建和存储字符串
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 将字符串存储在 constexpr 结构中
- 为什么我的结构在包含字符串时崩溃?
- 如何将字符串字符与结构数组进行比较?
- 无法使用 strcpy 将字符串复制到静态结构成员
- c++ 将函数参数(带有模板变量的结构)转换为函数体内的字符串
- 在 c++ 中使用字符串变量选择静态类结构,而不带对象
- 如何输出属于向量数组的结构字符串
- 为什么我不能像正常一样初始化 typedef 结构字符串或字符?
- 如何引用结构字符串成员
- c++运算符+=重载结构字符串
- 如何将平面列表合并转换为层次结构字符串
- 如何在c++中比较两个结构字符串