iostream

Why is std::endl generating this cryptic error message?

若如初见. 提交于 2020-01-02 02:26:05
问题 If I try to compile the following code I get the following compiler error (see code.) It compiles without error if std::endl is removed. #include <iostream> #include <sstream> #include <utility> namespace detail { template <class T> void print(std::ostream& stream, const T& item) { stream << item; } template <class Head, class... Tail> void print(std::ostream& stream, const Head& head, Tail&&... tail) { detail::print(stream, head); detail::print(stream, std::forward<Tail>(tail)...); } }

Is there a way to get non-locking stream insertion/extraction on basic_iostream in Windows?

[亡魂溺海] 提交于 2020-01-02 01:54:07
问题 I'm a C++ developer who has primarily programmed on Solaris and Linux until recently, when I was forced to create an application targeted to Windows. I've been using a communication design based on C++ I/O stream backed by TCP socket. The design is based on a single thread reading continuously from the stream (most of the time blocked in the socket read waiting for data) while other threads send through the same stream (synchronized by mutex). When moving to windows, I elected to use the

gcc: Strip unused functions

左心房为你撑大大i 提交于 2020-01-02 00:14:10
问题 I noticed that sometimes even if I don't use iostream and related I/O libraries, my binaries produced by Mingw were still unreasonably large. For example, I wrote a code to use vector and cstdio only and compiled it with -O2 -flto , my program can go as large as 2MB! I run nm main.exe > e.txt and was shocked to see all the iostream related functions in it. After some googling, I learnt to use -ffunction-sections -Wl,-gc-sections , that reduces the program size from 2MB to ~300KB (if with -s ,

Redirect std::cout to a QTextEdit [duplicate]

。_饼干妹妹 提交于 2020-01-01 03:50:29
问题 This question already has answers here : redirect std::cout to QTextEdit (2 answers) Closed 6 years ago . I'm new to Qt application development, I would appreciate your help. This is a re-post of redirect std::cout to QTextEdit I'm trying to redirect std::cout to a QTextEdit and I've seen and tried to test the example provided in the following link. Reference Link 1: http://lists.trolltech.com/qt-interest/2005-06/thread00166-0.html Using Qt Creator 2.4.1 to test the example from Reference

When are parameters calculated, when having concatenated call: obj.F1().F2().F3( sin(x) )?

对着背影说爱祢 提交于 2019-12-31 06:09:31
问题 I use the streaming operators (e.g. operator<<(const char*) ) for logging. In my unit tests I have a test like the following: MyLogger oLogger; oLogger << "charly"; oLogger << "foo" << sleep( 10 ) << "bar"; oLogger << "marcus"; First I executed a second thread, which should log at the same time. I was wondering, why between "foo" and "bar" no other log-output was generated. So I printed in each operator the current time . I expected something like this: 50 sec 137051 usec charly 50 sec 137930

Why will cin split a floating value into two parts?

岁酱吖の 提交于 2019-12-31 05:18:15
问题 I have a problem about cin. int main(void) { int a; float b; cin >> a >> b; } When I give a floating number (such as 3.14) as input, neither a nor b get the complete value (3.14): the output is a=3, b=0.14. I know that cin will split the input by space, tab or Return, but 'dot' will not, right? And why will the following code work? int main(void) { int i=0; int k=0; float j=0; cin >> i >> k >> j; // i =3, j=k=0 } And one more problem, what benefit will compiler do this for us? Thanks! 回答1:

Introduction To C++ IO Streams

强颜欢笑 提交于 2019-12-30 19:18:06
问题 I got a snippet of code from this article and I'm confused as to how it works? The snippet starts by saying: You can detect that a particular read or write operation failed by testing the result of the read. For example, to check that a valid integer is read from the user, you can do this: int x; if ( cin >> x ) { cout << "Please enter a valid number" << endl; } This works because the read operation returns a reference to the stream. I understand that the cin >> x operation returns a

Introduction To C++ IO Streams

淺唱寂寞╮ 提交于 2019-12-30 19:16:51
问题 I got a snippet of code from this article and I'm confused as to how it works? The snippet starts by saying: You can detect that a particular read or write operation failed by testing the result of the read. For example, to check that a valid integer is read from the user, you can do this: int x; if ( cin >> x ) { cout << "Please enter a valid number" << endl; } This works because the read operation returns a reference to the stream. I understand that the cin >> x operation returns a

Infix vs prefix syntax: name lookup differences

拟墨画扇 提交于 2019-12-30 17:21:51
问题 Operators in C++ are usually considered to be an alternative syntax for functions/methods, especially in the context of overloading. If so, the two expressions below should be synonymous: std::cout << 42; operator<<(std::cout, 42); In practise, the second statement leads to the following error: call of overloaded ‘operator<<(std::ostream&, int)’ is ambiguous As usual, such error message is accompanied with a list of possible candidates, these are: operator<<(basic_ostream<_CharT, _Traits>& _

How can I derive my own stream from a standard stream?

浪尽此生 提交于 2019-12-30 09:09:33
问题 How can I derive my own stream from a standard stream? In C# language, there is a Stream class, but C++'s streams are too complex. I want something like this: class my_stream : public std::stream { // How to derive? }; void using_a_stream(std::stream* s) { *s << "Hello world"; } void main() { std::stream s1; std::fstream s2("C:\\test.txt"); my_stream s3; using_a_stream(&s1); using_a_stream(&s2); using_a_stream(&s3); } Note: The code just a sample and may be invalid C++ program. Thanks. 回答1: I