c++-faq

Undefined behavior and sequence points reloaded

社会主义新天地 提交于 2019-11-26 01:44:17
问题 Consider this topic a sequel of the following topic: Previous installment Undefined behavior and sequence points Let\'s revisit this funny and convoluted expression (the italicized phrases are taken from the above topic *smile* ): i += ++i; We say this invokes undefined-behavior. I presume that when say this, we implicitly assume that type of i is one of the built-in types. What if the type of i is a user-defined type? Say its type is Index which is defined later in this post (see below).

When do I use a dot, arrow, or double colon to refer to members of a class in C++?

喜你入骨 提交于 2019-11-26 01:40:49
问题 Coming from other C-derived languages (like Java or C#) to C++, it is at first very confusing that C++ has three ways to refer to members of a class: a::b , a.b , and a->b . When do I use which one of these operators? (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

What are the new features in C++17?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 01:38:15
问题 C++17 is now feature complete, so unlikely to experience large changes. Hundreds of proposals were put forward for C++17. Which of those features were added to C++ in C++17? When using a C++ compiler that supports \"C++1z\", which of those features are going to be available when the compiler updates to C++17? 回答1: Language features: Templates and Generic Code Template argument deduction for class templates Like how functions deduce template arguments, now constructors can deduce the template

What is std::move(), and when should it be used?

半世苍凉 提交于 2019-11-26 01:35:18
问题 What is it? What does it do? When should it be used? Good links are appreciated. 回答1: Wikipedia Page on C++11 R-value references and move constructors In C++11, in addition to copy constructors, objects can have move constructors. (And in addition to copy assignment operators, they have move assignment operators.) The move constructor is used instead of the copy constructor, if the object has type "rvalue-reference" ( Type && ). std::move() is a cast that produces an rvalue-reference to an

Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

China☆狼群 提交于 2019-11-26 01:29:24
问题 Does C++ support \'finally\' blocks? What is the RAII idiom? What is the difference between C++\'s RAII idiom and C#\'s \'using\' statement? 回答1: No, C++ does not support 'finally' blocks. The reason is that C++ instead supports RAII: "Resource Acquisition Is Initialization" -- a poor name † for a really useful concept. The idea is that an object's destructor is responsible for freeing resources. When the object has automatic storage duration, the object's destructor will be called when the

How should I write ISO C++ Standard conformant custom new and delete operators?

拥有回忆 提交于 2019-11-26 01:28:23
问题 How should I write ISO C++ standard conformant custom new and delete operators? This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ, Operator overloading, and its follow-up, Why should one replace default new and delete operators? Section 1: Writing a standard-conformant new operator Part 1: Understanding the requirements for writing a custom new operator Part 2: Understanding the new_handler requirements Part 3: Understanding specific scenario

Polymorphism in C++

♀尐吖头ヾ 提交于 2019-11-26 01:27:35
问题 AFAIK: C++ provides three different types of polymorphism. Virtual functions Function name overloading Operator overloading In addition to the above three types of polymorphism, there exist other kinds of polymorphism: run-time compile-time ad-hoc polymorphism parametric polymorphism I know that runtime polymorphism can be achieved by virtual functions and static polymorphism can be achieved by template functions But for the other two ad-hoc polymorphism parametric polymorphism the website

How to implement classic sorting algorithms in modern C++?

余生长醉 提交于 2019-11-26 01:20:15
问题 The std::sort algorithm (and its cousins std::partial_sort and std::nth_element ) from the C++ Standard Library is in most implementations a complicated and hybrid amalgamation of more elementary sorting algorithms, such as selection sort, insertion sort, quick sort, merge sort, or heap sort. There are many questions here and on sister sites such as https://codereview.stackexchange.com/ related to bugs, complexity and other aspects of implementations of these classic sorting algorithms. Most

Do the parentheses after the type name make a difference with new?

◇◆丶佛笑我妖孽 提交于 2019-11-26 01:17:49
问题 If \'Test\' is an ordinary class, is there any difference between: Test* test = new Test; and Test* test = new Test(); 回答1: Let's get pedantic, because there are differences that can actually affect your code's behavior. Much of the following is taken from comments made to an "Old New Thing" article. Sometimes the memory returned by the new operator will be initialized, and sometimes it won't depending on whether the type you're newing up is a POD (plain old data), or if it's a class that

What are the differences between a pointer variable and a reference variable in C++?

丶灬走出姿态 提交于 2019-11-26 01:17:21
问题 I know references are syntactic sugar, so code is easier to read and write. But what are the differences? Summary from answers and links below: A pointer can be re-assigned any number of times while a reference cannot be re-assigned after binding. Pointers can point nowhere ( NULL ), whereas a reference always refers to an object. You can\'t take the address of a reference like you can with pointers. There\'s no \"reference arithmetic\" (but you can take the address of an object pointed by a