在单个语句中将临时字符串流转换为c_str() [英] Turning temporary stringstream to c_str() in single statement

查看:102
本文介绍了在单个语句中将临时字符串流转换为c_str()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下功能:

void f(const char* str);

假设我想使用stringstream生成一个字符串并将其传递给此函数。如果我想在一条语句中执行此操作,则可以尝试:

Suppose I want to generate a string using stringstream and pass it to this function. If I want to do it in one statement, I might try:

f((std::ostringstream() << "Value: " << 5).str().c_str()); // error

这会产生错误:'str()'不是'basic_ostream'的成员。好的,所以运算符<<返回的是ostream而不是ostringstream-如何将其强制转换回ostringstream?

This gives an error: 'str()' is not a member of 'basic_ostream'. OK, so operator<< is returning ostream instead of ostringstream - how about casting it back to an ostringstream?

1)这种强制转换安全吗?

1) Is this cast safe?

f(static_cast<std::ostringstream&>(std::ostringstream() << "Value: " << 5).str().c_str()); // incorrect output

现在,对于操作符来说,结果是<<(( Value: )调用,实际上是在调用ostream的操作符<<(void *)并打印一个十六进制地址。

Now with this, it turns out for the operator<<("Value: ") call, it's actually calling ostream's operator<<(void*) and printing a hex address. This is wrong, I want the text.

2)为什么operator<<在临时std :: ostringstream()上调用ostream运算符?

2) Why does operator<< on the temporary std::ostringstream() call the ostream operator? Surely the temporary has a type of 'ostringstream' not 'ostream'?

我可以强制转换临时对象以强制执行正确的运算符调用!

I can cast the temporary to force the correct operator call too!

f(static_cast<std::ostringstream&>(static_cast<std::ostringstream&>(std::ostringstream()) << "Value: " << 5).str().c_str());

这似乎可行并将值:5传递给f( )。

This appears to work and passes "Value: 5" to f().

3)我现在依赖未定义的行为吗?

3) Am I relying on undefined behavior now? The casts look unusual.

我知道最好的选择是这样的:

I'm aware the best alternative is something like this:

std::ostringstream ss;
ss << "Value: " << 5;
f(ss.str().c_str());

...但是我对一行执行的行为感兴趣。假设有人想要创建一个(可疑的)宏:

...but I'm interested in the behavior of doing it in one line. Suppose someone wanted to make a (dubious) macro:

#define make_temporary_cstr(x) (static_cast<std::ostringstream&>(static_cast<std::ostringstream&>(std::ostringstream()) << x).str().c_str())

// ...

f(make_temporary_cstr("Value: " << 5));

此功能是否如预期?

推荐答案

您不能将临时流投射到 std :: ostringstream& 。它格式错误(编译器必须告诉您它是错误的)。不过,可以执行以下操作:

You cannot cast the temporary stream to std::ostringstream&. It is ill-formed (the compiler must tell you that it is wrong). The following can do it, though:

f(static_cast<std::ostringstream&>(
  std::ostringstream().seekp(0) << "Value: " << 5).str().c_str());

那当然是丑陋的。但它显示了它如何工作。 seekp 是一个返回 std :: ostream& 的成员函数。一般情况下,最好写上这样的

That of course is ugly. But it shows how it can work. seekp is a member function returning a std::ostream&. Would probably better to write this generally

template<typename T>
struct lval { T t; T &getlval() { return t; } };

f(static_cast<std::ostringstream&>(
  lval<std::ostringstream>().getlval() << "Value: " << 5).str().c_str());

没有任何东西就需要 void * ,是因为 operator<< 是成员函数。带有 char const * operator <<不是。

The reason that without anything it takes the void*, is because that operator<< is a member-function. The operator<< that takes a char const* is not.

这篇关于在单个语句中将临时字符串流转换为c_str()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆