c++-faq

What happens when an exception goes unhandled in a multithreaded C++11 program?

扶醉桌前 提交于 2019-11-28 16:38:10
If I have a C++11 program running two threads, and one of them throws an unhandled exception, what happens? Will the entire program die a fiery death? Will the thread where the exception is thrown die alone (and if so, can I obtain the exception in this case)? Something else entirely? Nothing has really changed. The wording in n3290 is: If no matching handler is found, the function std::terminate() is called The behavior of terminate can be customized with set_terminate , but: Required behavior : A terminate_handler shall terminate execution of the program without returning to the caller. So

What C++ idioms should C++ programmers use? [closed]

懵懂的女人 提交于 2019-11-28 15:09:00
What C++ idioms should C++ programmers know? By C++ idioms, I mean design patterns or way of doing certain things that are only applicable for C++ or more applicable for C++ than most other languages. Why one should use the idioms, and what do the idioms accomplish? Here is one list . If I had to pick a couple I might go with the Curiously Recurring Template Pattern or Virtual Contstructors. By far the single most important "pattern" to learn and know that's (nearly) unique to C++ is RAII (Resource Acquisition Is Initialization). Edit: (To answer extra question edited into the question). You

Which C++ idioms are deprecated in C++11?

流过昼夜 提交于 2019-11-28 14:54:22
With the new standard, there are new ways of doing things, and many are nicer than the old ways, but the old way is still fine. It's also clear that the new standard doesn't officially deprecate very much, for backward compatibility reasons. So the question that remains is: What old ways of coding are definitely inferior to C++11 styles, and what can we now do instead? In answering this, you may skip the obvious things like "use auto variables". Sumant Final Class : C++11 provides the final specifier to prevent class derivation C++11 lambdas substantially reduce the need for named function

What's the difference between the terms “source file” and “translation unit”?

ⅰ亾dé卋堺 提交于 2019-11-27 21:08:18
问题 What's the difference between source file and translation unit? 回答1: From the C++ Standard: A source file together with all the headers and source files included via the preprocessing directive #include less any source line skipped by any of the conditional inclusion preprocessing directives is called a translation unit. 回答2: A "translation unit" is a source file plus any headers or other source files it #includes, plus any files that THEY include, and so on. A source file is just that...one

Using RAII to manage resources from a C-style API

社会主义新天地 提交于 2019-11-27 20:34:02
Resource Acquisition is Initialization (RAII) is commonly used in C++ to manage the lifetimes of resources which require some manner of cleanup code at the end of their lifetime, from delete ing new ed pointers to releasing file handles. How do I quickly and easily use RAII to manage the lifetime of a resource I acquire from a C-style API? In my case, I want to use RAII to automatically execute a cleanup function from a C-style API when the variable holding the C-style resource it releases goes out of scope. I don't really need additional resource wrapping beyond that, and I'd like to minimize

Meaning of default initialization changed in C++11?

旧街凉风 提交于 2019-11-27 20:24:38
C++2003 8.5/5 says: To default-initialize an object of type T means: — if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); — if T is an array type, each element is default-initialized; — otherwise, the object is zero-initialized . [Emphasis added.] The C++2011 standard changed that last item to — otherwise, no initialization is performed . This seems like it would be a breaking change for some programs. Was this intentional? Edit Here's some code to motivate this question: class Foo

What are the stages of compilation of a C++ program?

守給你的承諾、 提交于 2019-11-27 17:18:38
Are the stages of compilation of a C++ program specified by the standard? If so, what are they? If not, an answer for a widely-used compiler (I'd prefer MSVS) would be great. I'm talking about preprocessing, tokenization, parsing and such. What is the order in which they are executed and what do they do in particular? EDIT: I know what compilation, linking and preprocessing do, I'm mostly interested in the others and the order. Explanations for these are, of course, also welcomed since I might not be the only one interested in an answer. Keith Thompson Are the stages of compilation of a C++

What is the <=> operator in C++?

耗尽温柔 提交于 2019-11-27 16:57:23
While I was trying to learn about C++ operators, I stumbled upon a strange comparison operator on cppreference.com , * in a table that looked like this: "Well, if these are common operators in C++, I better learn them", I thought. But all my attempts to elucidate this mystery were unsuccessful. Even here, on Stack Overflow I had no luck in my search. Is there a connection between <=> and C++ ? And if there is, what does this operator do exactly? * In the meantime cppreference.com updated that page and now contains information about the <=> operator. This is called the three-way comparison

Restrict variadic template arguments

自古美人都是妖i 提交于 2019-11-27 11:02:12
Can we restrict variadic template arguments to a certain type? I.e., achieve something like this (not real C++ of course): struct X {}; auto foo(X... args) Here my intention is to have a function which accepts a variable number of X parameters. The closest we have is this: template <class... Args> auto foo(Args... args) but this accepts any type of parameter. Yes it is possible. First of all you need to decide if you want to accept only the type, or if you want to accept a implicitly convertible type. I use std::is_convertible in the examples because it better mimics the behavior of non

Conversion from Derived** to Base**

丶灬走出姿态 提交于 2019-11-27 09:26:20
I was reading this and unfortunately could not understand in depth why the compiler does not allow conversion from Derived** to Base**. Also I have seen this which gives no more info than the parashift.com's link. EDIT: Let us analyze this code line by line: Car car; Car* carPtr = &car; Car** carPtrPtr = &carPtr; //MyComment: Until now there is no problem! Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++ //MyComment: Here compiler gives me an error! And I try to understand why. //MyComment: Let us consider that it was allowed. So what?? Let's go ahead! NuclearSubmarine sub;