ostream

Class to output to several ostreams (file and console)

淺唱寂寞╮ 提交于 2019-12-22 10:46:08
问题 Right, I'm not even sure how to correctly formulate this; I feel it's a bit of an involved question. I'm sure someone can help me out here though. This is what I want to do: Have a single class that I can send stuff to, like this. icl << "Blah blah blah" << std::endl; I want to be able to .attach() classes that inherit std::basic_ostream to it. Those classes would then be able to format the output their own way. One might add a timestamp and write to a log, the other might write it to the

Why is ranges::ostream_iterator default-constructible?

独自空忆成欢 提交于 2019-12-22 05:13:29
问题 This question follows a discussion in the comments here. In Eric Niebler's ranges-v3 library (which is sort-of becoming part of the standard for C++20), ranges::ostream_iterator is default-constructible - without an ostream. How come? I thought that "dummy" construction with effective construction later is an anti-pattern in C++, a wart we are gradually getting rid of. std::ostream iterator can only be constructed with a stream (for now - before C++20). And it's not as though we can do

A bug with the nifty-counter idiom or an ill-formed static order fiasco?

三世轮回 提交于 2019-12-21 20:14:31
问题 The following code crashes with clang (version 5.0.0-3~16.04.1 on x86_64-pc-linux-gnu) but works fine with gcc (9.2.0). struct Registry { static int registerType(int type) { std::cout << "registering: " << type; return type; } }; template<typename T> struct A { static int i; }; template<typename T> int A<T>::i = Registry::registerType(9); int main() { std::cout << A<int>::i << std::endl; } The clang crash, is according to address sanitizer due to: ASAN:DEADLYSIGNAL ===========================

C++ template/ostream operator question

孤者浪人 提交于 2019-12-21 12:24:54
问题 trying to get the operator to work, but throwing me bunch of errors: my header file template <unsigned short n> class Vector { public: std::vector<float> coords; Vector(); Vector(std::vector<float> crds); friend std::ostream& operator <<(std::ostream& out, const Vector& v); }; template <unsigned short n> Vector<n>::Vector() { coords.assign(n, 0.0); } template <unsigned short n> std::ostream& operator<<(std::ostream& out, const Vector<n>& v) { out << "(" << v.coords[1] << " - " << v.coords[2]

creating an ostream

我的未来我决定 提交于 2019-12-19 08:05:22
问题 I am trying to create a c++ ostream for educational reasons. My test will be creating an ostream that acts like a ofstream would except instead of writing to a file it would write to a deque or vector container. 回答1: As it is for education, as you say, i will show you how i would do such a thingy. Otherwise, stringstream is really the way to go. Sounds like you want to create a streambuf implementation that then writes to a vector / deque. Something like this (copying from another answer of

C++ cout with prefix

半世苍凉 提交于 2019-12-19 04:55:34
问题 I want a ostream with a prefix at the beginning of every line redirected on cout; I try this: #include <iostream> #include <thread> class parallel_cout : public std::ostream { public: parallel_cout(std::ostream& o):out(o){} template <typename T> std::ostream& operator<< (const T& val) { out << "prefix " << val; return *this; } std::ostream& out; }; int main() { parallel_cout pc(std::cout); pc<<"a\nb"<<"c\n"; } but i have in output prefix a b without c. why this? 回答1: The way you modify the

In simple terms, what is the purpose of flush() in ostream

给你一囗甜甜゛ 提交于 2019-12-19 03:46:33
问题 By definition taken from: http://www.cplusplus.com/reference/iostream/ostream/flush/ , it is not clear why the function exists, and for what purpose you would use it for. Why not call flush(), every time your write to the stream? 回答1: In all likelihood, the word flush comes from exactly what you'd flush in real-life. A toilet... So let's try a toilet analogy: Flushing every time a new one drops into the bowl is very time-consuming and a complete waste of water. That's a big problem today

identifier “ostream” is undefined error [closed]

£可爱£侵袭症+ 提交于 2019-12-18 22:32:11
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . i need to implement a number class that support operator << for output. i have an error: "identifier "ostream" is undefined" from some reason eventhough

C++ : friend declaration ‘declares a non-template function

半世苍凉 提交于 2019-12-17 19:46:41
问题 I have a problem to overload the << stream operator and I don't find the solution : template<class T, unsigned int TN> class NVector { inline friend std::ostream& operator<< ( std::ostream &lhs, const NVector<T, TN> &rhs); }; template<class T, unsigned int TN> inline std::ostream& NVector<T, TN>::operator<<( std::ostream &lhs, const NVector<T, TN> &rhs) { /* SOMETHING */ return lhs; }; It produces the following error message: warning : friend declaration ‘std::ostream& operator<<(std::ostream

The result of int c=0; cout<<c++<<c;

此生再无相见时 提交于 2019-12-17 16:50:39
问题 I think it should be 01 but someone says its "undefined", any reason for that? 回答1: c++ is both an increment and an assignment. When the assignment occurs (before or after other code on that line) is left up to the discretion of the compiler. It can occur after the cout << or before. This can be found in the C99 standard http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf You can find it on page 28 in the pdf or section 5.1.2.3 the actual increment of p can occur at any time between the