c++-faq

How to get IOStream to perform better?

牧云@^-^@ 提交于 2019-11-26 03:04:18
问题 Most C++ users that learned C prefer to use the printf / scanf family of functions even when they\'re coding in C++. Although I admit that I find the interface way better (especially POSIX-like format and localization), it seems that an overwhelming concern is performance. Taking at look at this question: How can I speed up line by line reading of a file It seems that the best answer is to use fscanf and that the C++ ifstream is consistently 2-3 times slower. I thought it would be great if we

Why is `std::move` named `std::move`?

丶灬走出姿态 提交于 2019-11-26 02:40:20
问题 The C++11 std::move(x) function doesn\'t really move anything at all. It is just a cast to r-value. Why was this done? Isn\'t this misleading? 回答1: It is correct that std::move(x) is just a cast to rvalue - more specifically to an xvalue, as opposed to a prvalue. And it is also true that having a cast named move sometimes confuses people. However the intent of this naming is not to confuse, but rather to make your code more readable. The history of move dates back to the original move

Why does volatile exist?

故事扮演 提交于 2019-11-26 02:39:25
问题 What does the volatile keyword do? In C++ what problem does it solve? In my case, I have never knowingly needed it. 回答1: volatile is needed if you are reading from a spot in memory that, say, a completely separate process/device/whatever may write to. I used to work with dual-port ram in a multiprocessor system in straight C. We used a hardware managed 16 bit value as a semaphore to know when the other guy was done. Essentially we did this: void waitForSemaphore() { volatile uint16_t* semPtr

Convert string to int with bool/fail in C++

送分小仙女□ 提交于 2019-11-26 02:37:43
问题 I have a std::string which could be a string or could be a value (such as 0 ). What is the best or easiest way to convert the std::string to int with the ability to fail? I want a C++ version of C#\'s Int32.TryParse . 回答1: Use boost::lexical_cast. If the cast cannot be done, it will throw an exception. #include <boost/lexical_cast.hpp> #include <iostream> #include <string> int main(void) { std::string s; std::cin >> s; try { int i = boost::lexical_cast<int>(s); /* ... */ } catch(...) { /* ...

Copy constructor and = operator overload in C++: is a common function possible?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 02:27:27
问题 Since a copy constructor MyClass(const MyClass&); and an = operator overload MyClass& operator = (const MyClass&); have pretty much the same code, the same parameter, and only differ on the return, is it possible to have a common function for them both to use? 回答1: Yes. There are two common options. One - which is generally discouraged - is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a

Why do I see strange values when I print uninitialized variables?

天大地大妈咪最大 提交于 2019-11-26 02:24:48
问题 In the following code, the variable has no initial value and printed this variable. int var; cout << var << endl; output : 2514932 double var; cout << var << endl; output : 1.23769e-307 I don\'t understand these output numbers. Can any one explain this to me? 回答1: Put simply, var is not initialized and reading an uninitialized variable leads to undefined behavior. So don't do it. The moment you do, your program is no longer guaranteed to do anything you say. Formally, "reading" a value means

Does const mean thread-safe in C++11?

血红的双手。 提交于 2019-11-26 02:04:57
问题 I hear that const means thread-safe in C++11 . Is that true? Does that mean const is now the equivalent of Java \'s synchronized ? Are they running out of keywords ? 回答1: I hear that const means thread-safe in C++11 . Is that true? It is somewhat true... This is what the Standard Language has to say on thread-safety: [1.10/4] Two expression evaluations conflict if one of them modifies a memory location (1.7) and the other one accesses or modifies the same memory location. [1.10/21] The

Overloading member access operators ->, .* (C++)

拈花ヽ惹草 提交于 2019-11-26 02:04:38
问题 I understand most operator overloading, with the exception of the member access operators -> , .* , ->* etc. In particular, what is passed to these operator functions, and what should be returned? How does the operator function (e.g. operator->(...) ) know what member is being refered to? Can it know? Does it even need to know? Finally, are there any const considerations that need to be taken into account? For example, when overloading something like operator[] , generally you will need both

Object destruction in C++

為{幸葍}努か 提交于 2019-11-26 01:57:48
问题 When exactly are objects destroyed in C++, and what does that mean? Do I have to destroy them manually, since there is no Garbage Collector? How do exceptions come into play? (Note: This is meant to be an entry to Stack Overflow\'s C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place,

Is the practice of returning a C++ reference variable evil?

怎甘沉沦 提交于 2019-11-26 01:48:11
问题 This is a little subjective I think; I\'m not sure if the opinion will be unanimous (I\'ve seen a lot of code snippets where references are returned). According to a comment toward this question I just asked, regarding initializing references, returning a reference can be evil because, [as I understand] it makes it easier to miss deleting it, which can lead to memory leaks. This worries me, as I have followed examples (unless I\'m imagining things) and done this in a fair few places... Have I