language-lawyer

What formally guarantees that non-atomic variables can't see out-of-thin-air values and create a data race like atomic relaxed theoretically can?

▼魔方 西西 提交于 2019-12-29 01:17:33
问题 This is a question about the formal guarantees of the C++ standard. The standard points out that the rules for std::memory_order_relaxed atomic variables allow "out of thin air" / "out of the blue" values to appear. But for non-atomic variables, can this example have UB? Is r1 == r2 == 42 possible in the C++ abstract machine? Neither variable == 42 initially so you'd expect neither if body should execute, meaning no writes to the shared variables. // Global state int x = 0, y = 0; // Thread 1

When do we need to have a default constructor?

喜你入骨 提交于 2019-12-28 11:52:40
问题 My question is simple. When do we need to have a default constructor? Please refer to the code below: class Shape { int k; public: Shape(int n) : k(n) {} ~Shape() {} }; class Rect : public Shape { int l; public: Rect(int n): l(n) {} //error C2512: 'Shape' : no appropriate default constructor available ~Rect() {} }; Why is the compiler not generating the zero argument default constructor implicitly in the class Rect? As per my knowledge, if a class (Rect) is derived from another class (Shape)

What's the exact semantics of deleted member functions in C++11?

强颜欢笑 提交于 2019-12-28 09:04:27
问题 struct A { A(); A(const A&); A& operator =(const A&); A(A&&) = delete; A& operator =(A&&) = delete; }; struct B { B(); B(const B&); B& operator =(const B&); }; int main() { A a; a = A(); // error C2280 B b; b = B(); // OK } My compiler is VC++ 2013 RC. error C2280: 'A &A::operator =(A &&)' : attempting to reference a deleted function I just wonder why the compiler doesn't try A& operator =(const A&); when A& operator =(A&&) is deleted? Is this behavior defined by the C++ standard? 回答1: a = A(

Is it undefined behavior to dereference a dangling pointer?

一个人想着一个人 提交于 2019-12-28 06:46:09
问题 I can't find where in the standard that it says this program is undefined: #include <iostream> int main() { int *p; { int n = 45; p = &n; } std::cout << *p; } None of the cases in §3.8 object lifetime seem to apply here. 回答1: I'm not 100% sure because of the wording but it looks like this is covered by 3.8/6 (the reason I think this interpretation is correct is because of the non-normative example in 3.8/5, // undefined behavior, lifetime of *pb has ended ): ...after the lifetime of an object

Is char default-promoted?

▼魔方 西西 提交于 2019-12-28 05:52:52
问题 This may be a silly question, but could someone please provide a standard reference for C++11 and C11: Is char default-promoted to int ? Here's a little background: Both C and C++ have notions of default argument promotion (C++11: 5.2.2/7; C11: 6.5.2.2/6). This entails that in the following call, the arguments are promoted: void f(int, ...); float a = 1; short int b = 2; char c = 'x'; f(0, a, b, c); For the function call, a is converted to double and b is converted to int . But what happens

Meaning of default initialization changed in C++11?

佐手、 提交于 2019-12-28 05:43:11
问题 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

The behaviour of floating point division by zero

可紊 提交于 2019-12-28 03:52:04
问题 Consider #include <iostream> int main() { double a = 1.0 / 0; double b = -1.0 / 0; double c = 0.0 / 0; std::cout << a << b << c; // to stop compilers from optimising out the code. } I have always thought that a will be +Inf, b will be -Inf, and c will be NaN. But I also hear rumours that strictly speaking the behaviour of floating point division by zero is undefined and therefore the above code cannot considered to be portable C++. (That theoretically obliterates the integrity of my million

Does C++11 allow dollar signs in identifiers?

我怕爱的太早我们不能终老 提交于 2019-12-28 03:05:30
问题 Are dollar-signs allowed in identifiers in C++03? covers that dollar signs in identifiers are not allowed in C++03. GCC provides it as a C extension and properly gives a diagnostic in C++03 mode. However, in C++11, int $ = 0 will compile without warning. This answer reasons that $ may be allowed because no diagnostic is required for implementation defined identifiers: The answer here is "Maybe" : According to §2.11 , identifiers may consist of digits and identifier-nondigits , starting with

Does C++11 allow dollar signs in identifiers?

独自空忆成欢 提交于 2019-12-28 03:05:06
问题 Are dollar-signs allowed in identifiers in C++03? covers that dollar signs in identifiers are not allowed in C++03. GCC provides it as a C extension and properly gives a diagnostic in C++03 mode. However, in C++11, int $ = 0 will compile without warning. This answer reasons that $ may be allowed because no diagnostic is required for implementation defined identifiers: The answer here is "Maybe" : According to §2.11 , identifiers may consist of digits and identifier-nondigits , starting with

What's the behavior of an uninitialized variable used as its own initializer?

好久不见. 提交于 2019-12-28 01:31:12
问题 I noticed just now that the following code can be compiled with clang/gcc/clang++/g++, using c99 , c11 , c++11 standards. int main(void) { int i = i; } and even with -Wall -Wextra , none of the compilers even reports warnings. By modifying the code to int i = i + 1; and with -Wall , they may report: why.c:2:13: warning: variable 'i' is uninitialized when used within its own initialization [-Wuninitialized] int i = i + 1; ~ ^ 1 warning generated. My questions: Why is this even allowed by