15

Consider the following function:

void f(const char* str);

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

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) Is this cast safe?

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

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) 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());

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

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));

Would this function as expected?

AshleysBrain
  • 22,335
  • 15
  • 88
  • 124
  • Possible duplicate of [stringstream, string, and char\* conversion confusion](http://stackoverflow.com/questions/1374468/stringstream-string-and-char-conversion-confusion). – jww May 06 '15 at 06:29

6 Answers6

16

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());

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());

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.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Your snippet with seekp(0) returns an empty string for me - but the lval template appears to work correctly. That relies fully on defined behavior then, and would be portable? – AshleysBrain Mar 12 '10 at 14:13
  • @Ashley, sure it's all defined. – Johannes Schaub - litb Mar 12 '10 at 14:13
  • Could lval have an conversion operator to T&, avoiding the function call? (or perhaps the compiler doesn't pick up that operator automatically...) – Macke Mar 12 '10 at 15:11
  • @Macus, yep that wouldn't work. In free operators, template argument deduction would fail. And for member operators, those wouldn't be found in the first place. The conversion function isn't taken. – Johannes Schaub - litb Mar 12 '10 at 15:28
  • I don't get it. Ok, it compiles eventually. But is it going to really work? The result of the expression is going to be const char*, but to what is this pointer going to point? Is it guaranteed that the temporary ostringstream().str() instance still exists when the f() is being executed and the pointer passed to f is valid? – David L. Jan 15 '13 at 14:12
  • @JohannesSchaub-litb Visual Studio 2012 sets the failbit when calling `seekp(0)` on a fresh `ostringstream` – Gregory Pakosz Aug 08 '14 at 10:43
3

A temporary cannot be passed as a non-const reference to a function, that's why it does not find the correct streaming operator and instead takes the one with void* argument (it is a member function and thus calling it on a temporary is OK).

What comes to getting around the limitations by casting, I have a feeling that it is in fact UB, but I cannot say for sure. Someone else will surely quote the standard.

Tronic
  • 10,250
  • 2
  • 41
  • 53
  • I'm not passing the temporary ostringstream to f() - I'm only passing the result of c_str(), which is const (if it matters). Besides, even so, what about that means it picks the void* argument? What's different about it that allows it to be picked but not the const char* argument? – AshleysBrain Mar 12 '10 at 13:53
  • 2
    You are trying to pass a temporary ostringstream to ostream& operator<<(ostream&, char const*) and that fails (without the cast you made to fix it). ostream& ostream::operator<<(void const*) can be picked because it is a member function and calling member functions of temporaries is permitted. – Tronic Mar 12 '10 at 13:58
1

This can be done using a C++11 lambda function.

#include <iostream>
#include <sstream>

void f(const char * str)
{
    std::cout << str << std::endl;
}

std::string str(void (*populate)(std::ostream &))
{
    std::ostringstream stream;
    populate(stream);
    return stream.str();
}

int main(int argc, char * * args)
{
    f(str([](std::ostream & ss){ ss << "Value: " << 5;  }).c_str());
    return 0;
}

// g++ -std=c++11 main.cpp -o main
// ./main
// Value: 5
Eliott
  • 332
  • 3
  • 13
  • `#define TO_STR(MAC) ([=]{std::stringstream ss; ss << MAC; return std::string(ss.str());})()` –  Nov 03 '18 at 03:31
0

I use something a bit like this for logging.

#include <sstream>

using namespace std;

const char *log_text(ostringstream &os, ostream &theSame)
{
  static string ret; // Static so it persists after the call
  ret = os.str();
  os.str(""); // Truncate so I can re-use the stream
  return ret.c_str();
}


int main(int argc, char **argv)
{
  ostringstream  ss;
  cout << log_text(ss, ss << "My first message") << endl;
  cout << log_text(ss, ss << "Another message") << endl;
}

Output:

My first message

Another message

0

If you like one_lined statements you can write:

// void f(const char* str); 
f(static_cast<ostringstream*>(&(ostringstream() << "Value: " << 5))->str());

However you should prefer easier to maintain code as:

template <typename V>
  string NumberValue(V val)
  {
     ostringstream ss;
     ss << "Value: " << val;
     return ss.str();
  }
f(NumberValue(5));
Alain Rist
  • 827
  • 6
  • 11
0

Since C++17 we have folding expressions for templates that can be used to make generic string formatter that can accept everything that have stream support:

template <class... Args>
std::string format(Args &&... args)
{
    std::ostringstream ostr;

    (ostr << ... << args);

    return ostr.str();
}

The you can use it like: format("Int number: ", 42, " Float number: ", 3.14).c_str().

In C++20 std::format is coming.

Calmarius
  • 18,570
  • 18
  • 110
  • 157