c++-faq

Is the safe-bool idiom obsolete in C++11?

假如想象 提交于 2019-12-27 10:21:33
问题 This answer of @R. Martinho Fernandes shows, that the safe-bool idiom is apperently deprecated in C++11, as it can be replaced by a simple explicit operator bool() const; according to the standard quote in the answer §4 [conv] p3 : An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (§8.5). Certain language constructs require that an expression be converted to a Boolean value. An expression e

Is the safe-bool idiom obsolete in C++11?

扶醉桌前 提交于 2019-12-27 10:20:08
问题 This answer of @R. Martinho Fernandes shows, that the safe-bool idiom is apperently deprecated in C++11, as it can be replaced by a simple explicit operator bool() const; according to the standard quote in the answer §4 [conv] p3 : An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (§8.5). Certain language constructs require that an expression be converted to a Boolean value. An expression e

Why is enum class preferred over plain enum?

北城以北 提交于 2019-12-27 09:00:20
问题 I heard a few people recommending to use enum classes in C++ because of their type safety . But what does that really mean? 回答1: C++ has two kinds of enum : enum class es Plain enum s Here are a couple of examples how to declare them: enum class Color { red, green, blue }; // enum class enum Animal { dog, cat, bird, human }; // plain enum What is the difference between two? enum class es - enumerator names are local to the enum and their values do not implicitly convert to other types (like

What is an undefined reference/unresolved external symbol error and how do I fix it?

独自空忆成欢 提交于 2019-12-25 08:21:39
问题 What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them? Feel free to edit/add your own. 回答1: Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference): The precedence among the syntax rules of translation is specified by the following phases [see footnote] . Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set

C++17 lambda capture *this

穿精又带淫゛_ 提交于 2019-12-21 06:50:16
问题 C++17 will add copy capture of this object by value, with a capture specification of [*this]. How is this useful? How is it different than capturing this ? Can't this already be achieved in C++14 with [tmp = *this] ? Bonus for explaining why P0018R3 uses [=, tmp = *this] instead of [tmp = *this] in their example. If they had used [tmp = *this] , all the listed downsides of the C++14 solution would be eliminated. 回答1: How is it useful? It's useful when you need a copy of *this - for example,

What is the difference between the standard library and the standard template library?

瘦欲@ 提交于 2019-12-20 09:49:43
问题 I keep seeing reference to both the C++ standard Library and the C++ Standard Template Library (STL). What is the difference between them? Wikipedia mentions that they share some headers but that's about it. 回答1: The Standard Template Library (STL) is a library of containers, iterators, algorithms, and function objects, that was created by Alexander Stepanov; the SGI website has the canonical implementation and documentation. The standard library is the library that is part of C++; it

Initializing members with members

做~自己de王妃 提交于 2019-12-18 06:49:16
问题 This is a problem I come across often. The following examples illustrates it: struct A { int m_SomeNumber; }; struct B { B( A & RequiredObject ); private: A & m_RequiredObject; }; struct C { C( ); private: A m_ObjectA; B m_ObjectB; }; The implementation of the constructor of C looks something like this: C::C( ) : B( m_ObjectA ) { } Since the order of initialization is not defined, m_ObjectA might be uninitialized when the constructor of m_ObjectB is called, resulting in undefined behavior.

Initializing members with members

旧巷老猫 提交于 2019-12-18 06:49:11
问题 This is a problem I come across often. The following examples illustrates it: struct A { int m_SomeNumber; }; struct B { B( A & RequiredObject ); private: A & m_RequiredObject; }; struct C { C( ); private: A m_ObjectA; B m_ObjectB; }; The implementation of the constructor of C looks something like this: C::C( ) : B( m_ObjectA ) { } Since the order of initialization is not defined, m_ObjectA might be uninitialized when the constructor of m_ObjectB is called, resulting in undefined behavior.

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

我是研究僧i 提交于 2019-12-17 15:34:07
问题 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

Explain C++ SFINAE to a non-C++ programmer

只谈情不闲聊 提交于 2019-12-17 05:37:27
问题 What is SFINAE in C++? Can you please explain it in words understandable to a programmer who is not versed in C++? Also, what concept in a language like Python does SFINAE correspond to? 回答1: Warning: this is a really long explanation, but hopefully it really explains not only what SFINAE does, but gives some idea of when and why you might use it. Okay, to explain this we probably need to back up and explain templates a bit. As we all know, Python uses what's commonly referred to as duck