c++-faq

What XML parser should I use in C++? [closed]

为君一笑 提交于 2019-11-25 22:29:47
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I have XML documents that I need to parse and/or I need to build XML documents and write them to text (either files or memory). Since the C++ standard library does not have a library for this, what should I use? Note: This is intended to be a definitive, C++-FAQ-style question for this. So yes, it is a duplicate

How do I remove code duplication between similar const and non-const member functions?

孤街浪徒 提交于 2019-11-25 22:29:44
问题 Let\'s say I have the following class X where I want to return access to an internal member: class Z { // details }; class X { std::vector<Z> vecZ; public: Z& Z(size_t index) { // massive amounts of code for validating index Z& ret = vecZ[index]; // even more code for determining that the Z instance // at index is *exactly* the right sort of Z (a process // which involves calculating leap years in which // religious holidays fall on Tuesdays for // the next thousand years or so) return ret; }

Why is volatile not considered useful in multithreaded C or C++ programming?

大兔子大兔子 提交于 2019-11-25 22:22:58
问题 As demonstrated in this answer I recently posted, I seem to be confused about the utility (or lack thereof) of volatile in multi-threaded programming contexts. My understanding is this: any time a variable may be changed outside the flow of control of a piece of code accessing it, that variable should be declared to be volatile . Signal handlers, I/O registers, and variables modified by another thread all constitute such situations. So, if you have a global int foo , and foo is read by one

Pointer to class data member “::*”

半世苍凉 提交于 2019-11-25 22:22:56
问题 I came across this strange code snippet which compiles fine: class Car { public: int speed; }; int main() { int Car::*pSpeed = &Car::speed; return 0; } Why does C++ have this pointer to a non-static data member of a class? What is the use of this strange pointer in real code? 回答1: It's a "pointer to member" - the following code illustrates its use: #include <iostream> using namespace std; class Car { public: int speed; }; int main() { int Car::*pSpeed = &Car::speed; Car c1; c1.speed = 1; //

What do the following phrases mean in C++: zero-, default- and value-initialization?

Deadly 提交于 2019-11-25 22:22:34
问题 What do the following phrases mean in C++: zero-initialization, default-initialization, and value-initialization What should a C++ developer know about them? 回答1: One thing to realize is that 'value-initialization' is new with the C++ 2003 standard - it doesn't exist in the original 1998 standard (I think it might be the only difference that's more than a clarification). See Kirill V. Lyadvinsky's answer for the definitions straight from the standard. See this previous answer about the

What exactly is the “as-if” rule?

核能气质少年 提交于 2019-11-25 22:20:58
问题 As the title says, What exactly is the \"as-if\" rule? An typical answer one would get is: The rule that allows any and all code transformations that do not change the observable behavior of the program From time to time we keep getting behaviors from certain implementations which are attributed to this rule. Many a times wrongly. So, what exactly is this rule. The standard does not clearly mention this rule as a section or paragraph, so what exactly falls under the purview of this rule? To

Meaning of &#39;const&#39; last in a function declaration of a class?

柔情痞子 提交于 2019-11-25 22:19:28
问题 What is the meaning of const in declarations like these? The const confuses me. class foobar { public: operator int () const; const char* foo() const; }; 回答1: When you add the const keyword to a method the this pointer will essentially become a pointer to const object, and you cannot therefore change any member data. (Unless you use mutable , more on that later). The const keyword is part of the functions signature which means that you can implement two similar methods, one which is called

std::wstring VS std::string

社会主义新天地 提交于 2019-11-25 22:19:14
问题 I am not able to understand the differences between std::string and std::wstring . I know wstring supports wide characters such as Unicode characters. I have got the following questions: When should I use std::wstring over std::string ? Can std::string hold the entire ASCII character set, including the special characters? Is std::wstring supported by all popular C++ compilers? What is exactly a \" wide character \"? 回答1: string ? wstring ? std::string is a basic_string templated on a char ,

What&#39;s the difference between “STL” and “C++ Standard Library”?

坚强是说给别人听的谎言 提交于 2019-11-25 22:18:49
问题 Someone brought this article to my attention that claims (I\'m paraphrasing) the STL term is misused to refer to the entire C++ Standard Library instead of the parts that were taken from SGI STL. (...) it refers to the \"STL\", despite the fact that very few people still use the STL (which was designed at SGI). Parts of the C++ Standard Library were based on parts of the STL, and it is these parts that many people (including several authors and the notoriously error-ridden cplusplus.com)

What is “Argument-Dependent Lookup” (aka ADL, or “Koenig Lookup”)?

你说的曾经没有我的故事 提交于 2019-11-25 22:17:38
问题 What are some good explanations on what argument dependent lookup is? Many people also call it Koenig Lookup as well. Preferably I\'d like to know: Why is it a good thing? Why is it a bad thing? How does it work? 回答1: Koenig Lookup , or Argument Dependent Lookup , describes how unqualified names are looked up by the compiler in C++. The C++11 standard § 3.4.2/1 states: When the postfix-expression in a function call (5.2.2) is an unqualified-id, other namespaces not considered during the usual