I have an object that overrides the stream operator << to act as a stringstream to make console printing easy.
cout << obj << endl;
I want to test this functionality in a unit test by comparing the string output with an expected string. I currently accomplish this by:
stringStream ss;
ss << obj;
string objStr = ss.str();
EXPECT_EQ(objStr, "expected string output");
This is not particular readable and it is certainly not succinct. Is there any easier, shorter and simpler way to get the string representation of an obj?
You could write a small, generic utility function that does that thing for you:
#include <utility>
#include <string>
#include <sstream>
template<typename T>
std::string make_string(T const& o)
{
std::stringstream ss;
ss << o;
return ss.str();
}
And use it this way:
EXPECTED_EQ(make_string(obj), "expected string output");
Related
I use std::ostringstream for formatting strings, which inherits its << operators from ostream, and consequently they return an ostream and not an ostringstream, which means that I can't call ostringstream::str on the result. This usually isn't a problem, since I can typically do this:
ostringstream stream;
stream << whatever;
string str = stream.str();
But occasionally I need* to use it in just a single expression, which is more difficult. I could do
string str = ((ostringstream&) (ostringstream() << whatever)).str();
but bad things will happen if that << overload returns some ostream that isn't an ostringstream. The only other thing I can think to do is something like
string str = ([&] () -> string {ostringstream stream; stream << whatever; return stream.str();})();
but this can't possibly be efficient, and is certainly very bad c++ code.
*Okay, I don't need to, but it would be a lot more convenient to.
Using
string str = ([&] () -> string
{
ostringstream stream;
stream << whatever;
return stream.str();
})();
is ok. However, I think it will be better to name that operation, create a function with that name, and call the function.
namespace MyApp
{
template <typename T>
std::string to_string(T const& t)
{
ostringstream stream;
stream << t;
return stream.str();
}
}
and then use
string str = MyApp::to_string(whatever);
The only other thing I can think to do is something like
string str = ([&] () -> string {ostringstream stream; stream << whatever; return stream.str();})();
but this can't possibly be efficient, and is certainly very bad c++
code.
Actually, no. Once the optimizer has a look at this, it's exactly as fast as the original code. And it's not "very bad c++ code". It's known as an Immediately Invoked Lambda Expression, or IILE. It's an idiom, and actually quite decent practice.
It would be better formatted more like this, though:
// One of the benefits of IILEs is that `const` can be used more frequently
string const str = [&] {
ostringstream stream;
stream << whatever;
return stream.str();
}();
Some people prefer using std::invoke to invoke the lambda instead:
string const str = std::invoke([&] {
ostringstream stream;
stream << whatever;
return stream.str();
});
Assuming you got
std::string get_str(std::ostream& out) {
retrun static_cast<std::stringbuf*>(out.rdbuf())->str();
}
You could use
std:: string s = get_str(std::ostringstream{} << ...);
The following program prints 0.
#include <ostream>
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string subjectString("subject");
std::ostream tempStream(NULL);
tempStream << subjectString;
std::ostream& updatedStream = tempStream;
std::stringstream ss;
ss << updatedStream;
std::cout << ss.str() << std::endl;
return 0;
}
Why?
EDIT
As per Niall's sugesstion, I tried:
#include <ostream>
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string subjectString("subject");
std::stringbuf buffer;
std::ostream tempStream(&buffer);
buffer.sputn(subjectString.c_str(), subjectString.size());
std::stringstream ss;
ss << tempStream;
std::cout << ss.str() << std::endl;
return 0;
}
Even this prints an address. Not the actual string.
The construction of std::ostream requires a buffer (not NULL).
In addition, basic_stream objects are not copyable;
basic_ostream( const basic_ostream& rhs ) = delete;
Reference;
http://en.cppreference.com/w/cpp/io/basic_ostream/basic_ostream
Try something more like this;
// ...
std::stringbuf buffer;
std::ostream tempStream(&buffer);
// ...
To associate a buffer with the stream.
Following on some of the discussions and edits;
In general, I would not directly manipulate the buffer, you should rather use the stream instead tempStream.write(...). The exact details are beyond the immediate question/problem; being the initialisation of the first stream with the buffer and streaming content into that stream. If all you want to do with the code is check if the data is in the buffer, then you could use tempStream.rdbuf()->sgetn(...).
You have already mentioned that this is part of a larger problem.
In the context of some the comments here and the original problem; this could be a case in which direct manipulation of the buffer is needed (in much the same way as the stream would). Your implementation would need to be well tested since this is not "the usual" way of working with streams, but it can work; .rdbuf() is the manner in which you can get to the underlying buffer. I don't have an exact snippet (maybe that's another question), but you can "clear the buffer" by resetting the position(s) of the put and get areas to be the same (see the positioning, put and get area functions of the buffer - std::ostream::seekp was mentioned as being used to deal with this). I think you standard library implementation of stringstream could also offer some useful hints.
Because tempStream has no stream to hold anything.
ss.str() returns NULL (0).
I am attempting to use an overloaded function to get a string.
void get(char prompt[], int size,const std::string b = "")
{
std::cout << prompt << ": ";
std::cin.get(b, size);
std::cin.ignore(10, '\n');
}
Now I did just change the last argument from a character array to a string at the advice of another poster on this site, so I'm a little lost. I'm getting error message at the '.' in between cin and get. I've also tried cin.getline (I have no idea if there's a difference or what it is)
Error message : cannot convert parameter 1 from 'const std::string' to
'char *'
The error has a lot of other stuff, but I think that's the important bit.
I'm indirectly answering your question by suggesting an alternative method. Here's my implementation, along with an example of how to use it.
#include <string>
#include <iostream>
#include <ostream>
std::string get(std::string const& prompt)
{
std::string ret;
std::cout << prompt << ": ";
getline(std::cin, ret);
return ret;
}
int main()
{
std::cout << get("Please enter your answer") << std::endl;
// or
std::string prompt("Enter your answer");
std::string response = get(prompt);
std::cout << response << std::endl;
}
When working with strings, you need to use the free function getline from the string header, not the member function getline from iostream. So it would be std::getline(std::cin, b);.
That being said getline won't accept a const string as its argument for the simple reason that the whole point of calling getline is to write to the string. Also note that unless you make b a (non-const) reference, any changes you perform on b inside your get method will not be visible outside of the method since strings are copied if you pass them by value.
The difference between istream::get(char*, streamsize) and istream::getline(char*, streamsize) is that the latter discards the newline character (as does the getline method for strings) while the former does not.
http://www.cplusplus.com/reference/iostream/istream/get/
http://www.cplusplus.com/reference/iostream/istream/ignore/
your call to get() doesn't match any of the existing istream methods. it just may end up being recursive if it ever works?
#include <string>
namespace std {
//I am attempting to use an overloaded function to get a string.
class Ciostream : public iostream {
public:
void get(char prompt[], int size,const std::string b = "")
{
cout << prompt << ": ";
cin.get(b, size);
cin.ignore(10, '\n');
}
};
}
//1.cpp:11:28: error: no matching function for call to 'std::basic_istream<char>::get(const string&, int&)'
For a project, I'd like to use stringstream to carry on data. To achieve this goal, I have to pass some stringstream as parameter to some function, but when I output the stringstreams, I see something like an address.
The code :
#include <iostream>
#include <sstream>
void doStuff(const std::iostream& msg)
{
std::cerr << msg << std::endl;
}
int main(void)
{
doStuff(std::stringstream("av"));
}
The output is :
0xbff4eb40
Can someone explains why I get an address when passing an rvalue ?
And why can't I pass a stringstream by value ?
You probably want to access the string on which the stringstream is storing its data:
void doStuff(const std::stringstream& msg)
{
std::cerr << msg.str() << std::endl;
}
What is happening in your code is that iostreams contain a void* operator which returns 0 if the stream contains any error or has reached EOF, and another value otherwise. This is usefull for error checking.
When you try to write you stream to std::cerr, the compiler realizes that the stream can be converted to a void* using that operator, and that a void* can be written to a ostream(the operator<< has been defined), and therefore uses it.
Note that i changed the method's signature so that it receives an std::stringstream as an argument, since std::iostream::str is not defined(this method is only available on string streams).
You get an address because it (like other streams) has a conversion to void * (which is primarily useful as a Boolean, to see whether reading/writing the stream has failed).
You can't pass it by value, because streams (again, in general, not just stringstreams) don't support copying and/or assigning.
To print the content of the stream, you could do something like:
void dostuff(std::iostream &msg) {
std::cerr << msg.rdbuf() << "\n";
}
Edit: Here's a complete demo program:
#include <iostream>
#include <sstream>
void show(std::ostream &os) {
std::cout << os.rdbuf() << "\n";
}
int main(){
std::stringstream test("whatever");
show(test);
return 0;
}
When I execute it, the output I get is the expected "whatever".
I have an Image class which has the following implementation
friend std::ostream& operator << ( std::ostream &os,Image* &img);
So I can serialize it by calling
ostm << img; // which will write an string into the ostream.
Is it possible to get that string out of the ostream or serialize it directly into an string object?
Thanks!
The solutions worked like a charm. Thank you so much!
Yes, you can use a std::ostringstream.
E.g.
#include <sstream>
#include <string>
#include <stdexcept>
std::string Serialize( const Image& img )
{
std::ostringstream oss;
if (!(oss << img))
{
throw std::runtime_error("Failed to serialize image");
}
return oss.str();
}
Presumably your actual object is an iostream, or a stringstream. If an iostream, you can do this:
std::iostream ss;
ss << "Some text\nlol";
std::string all_of_it((std::istreambuf_iterator<char>(ss)), std::istreambuf_iterator<char>());
std::cout << all_of_it; // Outputs: "Some text", then "lol" on a new line;
You need the istreambuf_iterator, hence the requirement for a bidirectional stream like iostream. Whenever you have extraction as well as insertion, you should use this, or a stringstream (or an fstream if working with files).
For a stringstream, just use its .str() member function to obtain its buffer as a string.