c++-faq

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

二次信任 提交于 2019-11-26 09:52:15
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 ? 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 execution of a program contains a data race if it contains two conflicting actions in different threads, at least

Rationale of enforcing some operators to be members

时光总嘲笑我的痴心妄想 提交于 2019-11-26 08:25:44
问题 There are 4 operators in C++ which can be overloaded but cannot be overloaded as freestanding (aka nonmember, standalone) functions. These operators are: operator = operator () operator -> operator [] This thread explains perfectly well the rationale behind prohibiting operator = to be a nonmember function. Any ideas about the other three? 回答1: The four operators mentioned in the original posting, = , () , -> and [] , must indeed be implemented as non-static member functions (by respectively

FAQ: Why does dynamic_cast only work if a class has at least 1 virtual method?

一个人想着一个人 提交于 2019-11-26 08:24:44
问题 This does not compile in C++: class A { }; class B : public A { }; ... A *a = new B(); B *b = dynamic_cast<B*>(a); 回答1: Because dynamic_cast can only downcast polymorphic types, so sayeth the Standard. You can make your class polymoprphic by adding a virtual destructor to the base class. In fact, you probably should anyway (See Footnote). Else if you try to delete a B object through an A pointer, you'll evoke Undefined Behavior. class A { public: virtual ~A() {}; }; et voila! Footnote There

Passing shared pointers as arguments

妖精的绣舞 提交于 2019-11-26 07:54:43
问题 If I declare an object wrapped in a shared pointer: std::shared_ptr<myClass> myClassObject(new myClass()); then I wanted to pass it as an argument to a method: DoSomething(myClassObject); //the called method void DoSomething(std::shared_ptr<myClass> arg1) { arg1->someField = 4; } Does the above simply increment the shared_pt\'s reference count and everything is cool? Or does it leave a dangling pointer? Are you still supposed to do this?: DoSomething(myClassObject.Get()); void DoSomething(std

gcc/g++: “No such file or directory”

爱⌒轻易说出口 提交于 2019-11-26 06:59:03
问题 g++ gives me errors of the form: foo.cc:<line>:<column>: fatal error: <bar>: No such file or directory compilation terminated. It is the same when compiling C-programs with gcc . Why is that? Please note: This question has been asked many times before, but each time it was specific to the askers situation. This question\'s purpose is to have a question that others can be closed as duplicates of , once and for all; a FAQ . 回答1: Your compiler just tried to compile the file named foo.cc . Upon

When do extra parentheses have an effect, other than on operator precedence?

为君一笑 提交于 2019-11-26 06:58:20
问题 Parentheses in C++ are used in many places: e.g. in function calls and grouping expressions to override operator precedence. Apart from illegal extra parentheses (such as around function call argument lists), a general -but not absolute- rule of C++ is that extra parentheses never hurt : 5.1 Primary expressions [expr.prim] 5.1.1 General [expr.prim.general] 6 A parenthesized expression is a primary expression whose type and value are identical to those of the enclosed expression. The presence

What is the purpose of std::launder?

≯℡__Kan透↙ 提交于 2019-11-26 06:54:25
问题 P0137 introduces the function template std::launder and makes many, many changes to the standard in the sections concerning unions, lifetime, and pointers. What is the problem this paper is solving? What are the changes to the language that I have to be aware of? And what are we launder ing? 回答1: std::launder is aptly named, though only if you know what it's for. It performs memory laundering . Consider the example in the paper: struct X { const int n; }; union U { X x; float f; }; ... U u =

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

北城以北 提交于 2019-11-26 06:00:00
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 appearing in such a context is said to be contextually converted to bool and is well-formed if and only if

Why would one replace default new and delete operators?

放肆的年华 提交于 2019-11-26 05:57:16
问题 Why should would one replace the default operator new and delete with a custom new and delete operators? This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ: Operator overloading. An followup entry to this FAQ is: How should I write ISO C++ standard conformant custom new and delete operators? Note: The answer is based on lessons from Scott Meyers\' More Effective C++. (Note: This is meant to be an entry to Stack Overflow\'s C++ FAQ. If you want to

Resolve build errors due to circular dependency amongst classes

纵然是瞬间 提交于 2019-11-26 05:40:30
问题 I often find myself in a situation where I am facing multiple compilation/linker errors in a C++ project due to some bad design decisions (made by someone else :) ) which lead to circular dependencies between C++ classes in different header files (can happen also in the same file) . But fortunately(?) this doesn\'t happen often enough for me to remember the solution to this problem for the next time it happens again. So for the purposes of easy recall in the future I am going to post a