language-lawyer

Why does the compiler assume that these seemingly equal pointers differ?

ぐ巨炮叔叔 提交于 2019-12-22 03:17:49
问题 Looks like GCC with some optimization thinks two pointers from different translation units can never be same even if they are actually the same. Code: main.c #include <stdint.h> #include <stdio.h> int a __attribute__((section("test"))); extern int b; void check(int cond) { puts(cond ? "TRUE" : "FALSE"); } int main() { int * p = &a + 1; check( (p == &b) == ((uintptr_t)p == (uintptr_t)&b) ); check(p == &b); check((uintptr_t)p == (uintptr_t)&b); return 0; } b.c int b __attribute__((section("test

Explicit destructor call with decltype

回眸只為那壹抹淺笑 提交于 2019-12-22 03:15:26
问题 Consider the following snippet: struct Foo {}; int main() { Foo f; f.~decltype(f)(); // fine with clang, error with gcc f.~decltype(auto)(); // error with both clang and gcc } The rules for an explicit destructor call are handled by the standard grammar with pseudo-destructor-name which is defined as follows: pseudo-destructor-name: nested-name-specifier opt type-name :: ~ type-name nested-name-specifier template simple-template-id :: ~type-name ~ type-name ~ decltype-specifier And: decltype

In C++, What does “access” mean in the strict aliasing rule?

本秂侑毒 提交于 2019-12-22 03:15:23
问题 3.10/10 says: If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined: However, the term "access" is not defined anywhere. In this context does it mean read , or read or modify ? In the C standard it is unambiguously defined as read or modify . However in C++11 it seems to be used with different meanings at different times, for example: 1.9/8: Access to volatile objects are evaluated strictly according

Meaning of phrase “constructors do not have names” in the C++ Standard

放肆的年华 提交于 2019-12-22 01:25:08
问题 While trying to understand the phrase "constructors do not have names" in the C++ Standard, it seems like I found an error in clang. Could someone confirm this? VS2015 and gcc rejects this code, and I think they it are is correct. At least, this is the impression I get from §12.1[class.ctor]/2 in N4140: #include <iostream> class A { public: A() { std::cout << "A()" << '\n'; } }; int main() { A::A(); } §12.1[class.ctor]/2 in N4140: A constructor is used to initialize objects of its class type.

Is it undefined behaviour if multiple operands in a compound expression modify the same object?

陌路散爱 提交于 2019-12-22 01:23:48
问题 I vaguely remember reading somewhere that it is undefined behaviour if multiple operands in a compound expression modify the same object. I believe an example of this UB is shown in the code below however I've compiled on g++, clang++ and visual studio and all of them print out the same values and can't seem to produce unpredictable values in different compilers. #include <iostream> int a( int& lhs ) { lhs -= 4; return lhs; } int b( int& lhs ) { lhs *= 7; return lhs; } int c( int& lhs ) { lhs

will right hand side of an expression always evaluated first

纵饮孤独 提交于 2019-12-21 20:56:42
问题 Will right side always evaluated to ahead of left side? And then the result of right side will be passed on to left side. I am not talking about the exception such as A[i]=i++ I am talking about the normal cases: A[i] = (j+32+43 & K); A[j] != (A[j] + A[k]); will the right part of all these expression evaluated first and then the result is compared to the left side? (Always) 回答1: In general the order of evaluation of sub-expressions is unspecified, there are a few exceptions such as logical

Can one get a pointer to a complete object representation element from a pointer to a suboject?

半世苍凉 提交于 2019-12-21 17:52:42
问题 Let's consider this code: int i; int is[10]{}; unsigned char * p = reinterpret_cast<unsigned char*>(&i); //p defined to point to the object-representation of the first element of array ints unsigned char * ps = reinterpret_cast<unsigned char*>(&is[0]); p += sizeof(int); ps += sizeof(int); //now ps points to the end of ints[0] and p point to the end of i; p += sizeof(int); //Undefined behavior according to [expr.add] ps += sizeof(int); //Undefined behavior? unsigned char c = *ps;//Undefined

Why is single virtual inheritance not enough to resolve the dreaded diamond problem?

只谈情不闲聊 提交于 2019-12-21 17:45:14
问题 struct B { int i; }; struct D1 : virtual B {}; struct D2 : B {}; // <-- not virtual struct DD : D1, D2 {}; Having coded above, still the compiler demands D2 also to be virtual : DD d; d.i = 0; // error: request for member `i' is ambiguous What I don't understand is, once you have prompted compiler that B is virtual with respect to DD (via D1 ) then why it still i is ambiguous ? (If my memory serves correct, the older VC++ (in 2006), was capable enough to make out this just with single virtual

Sequence points when calling functions in C and undefined/unspecified behaviour

北城以北 提交于 2019-12-21 17:37:33
问题 I'm trying to pin down my understanding of sequence points in C -- just wanted to check something. At present, I believe that (1) is undefined whereas (2) is merely unspecified, on the basis that in (2), there are sequence points after evaluating the arguments for g and h (so we're not modifying i twice between sequence points), but the order of evaluation of the arguments of f is still unspecified. Is my understanding correct? #include <stdio.h> int g(int i) { return i; } int h(int i) {

Definition of standard-layout class in C++14

▼魔方 西西 提交于 2019-12-21 17:18:09
问题 A standard-layout class is defined in [class]/7 in C++14, as follows (emphasis is mine): A standard-layout class is a class that: (7.1) — has no non-static data members of type non-standard-layout class (or array of such types) or reference, (7.2) — has no virtual functions (10.3) and no virtual base classes (10.1), (7.3) — has the same access control (Clause 11) for all non-static data members, (7.4) — has no non-standard-layout base classes, (7.5) — either has no non-static data members in