language-lawyer

Placement new and assignment of class with const member

做~自己de王妃 提交于 2019-12-28 00:55:33
问题 Why is that undefined behaviour? struct s { const int id; // <-- const member s(int id): id(id) {} s& operator =(const s& m) { return *new(this) s(m); // <-- undefined behavior? } }; (Quote from the standard would be nice). This question arose from this answer. 回答1: There is nothing that makes the shown code snippet inherently UB. However, it is almost certain UB will follow immediately under any normal usage. From [basic.life]/8 (emphasis mine) If, after the lifetime of an object has ended

Do I really need to implement user-provided constructor for const objects?

筅森魡賤 提交于 2019-12-27 23:35:45
问题 I have the code: class A { public: A() = default; private: int i = 1; }; int main() { const A a; return 0; } It compiles fine on g++ (see ideone), but fails on clang++ with error: default initialization of an object of const type 'const A' requires a user-provided default constructor I reported this issue on LLVM bug-tracker and got it INVALID. I see it absolutly pointless to try to convince the clang developers. On the other side, I don't see the reason for such restriction. Can anyone

So why is i = ++i + 1 well-defined in C++11?

杀马特。学长 韩版系。学妹 提交于 2019-12-27 22:06:26
问题 I've seen the other similar questions and read the defect about it. But I still don't get it. Why is i = ++i + 1 well-defined in C++11 when i = i++ + 1 is not? How does the standard make this well defined? By my working out, I have the following sequenced before graph (where an arrow represents the sequenced before relationship and everything is a value computation unless otherwise specified): i = ++i + 1 ^ | assignment (side effect on i) ^ ^ | | ☆i ++i + 1 || ^ i+=1 | ^ 1 | ★assignment (side

So why is i = ++i + 1 well-defined in C++11?

别等时光非礼了梦想. 提交于 2019-12-27 22:04:32
问题 I've seen the other similar questions and read the defect about it. But I still don't get it. Why is i = ++i + 1 well-defined in C++11 when i = i++ + 1 is not? How does the standard make this well defined? By my working out, I have the following sequenced before graph (where an arrow represents the sequenced before relationship and everything is a value computation unless otherwise specified): i = ++i + 1 ^ | assignment (side effect on i) ^ ^ | | ☆i ++i + 1 || ^ i+=1 | ^ 1 | ★assignment (side

Pure virtual functions may not have an inline definition. Why?

守給你的承諾、 提交于 2019-12-27 17:07:52
问题 Pure virtual functions are those member functions that are virtual and have the pure-specifier ( = 0; ) Clause 10.4 paragraph 2 of C++03 tells us what an abstract class is and, as a side note, the following: [Note: a function declaration cannot provide both a pure-specifier and a definition —end note] [Example: struct C { virtual void f() = 0 { }; // ill-formed }; —end example] For those who are not very familiar with the issue, please note that pure virtual functions can have definitions but

VS2012 unsigned negate

半城伤御伤魂 提交于 2019-12-25 17:22:37
问题 In code that is supposed to be quite portable, in a many person, many environment project, I have to deal with a problem with code that negates unsigned int (and same problem in other places negating std::size_t ). The intent is to create the additive inverse of that value (modulo MAX_VAL+1 where MAX_VAL is the max value of the type) and I found other questions on this topic confirm that is the standard specified behavior. VS2012, (with compile time options I cannot easily learn) calls that

How does DISTINCT interact with ORDER BY?

时间秒杀一切 提交于 2019-12-25 07:29:31
问题 Consider the two tables below: user: ID | name ---+-------- 1 | Alice 2 | Bob 3 | Charlie event: order | user ------+------------ 1 | 1 (Alice) 2 | 2 (Bob) 3 | 3 (Charlie) 4 | 3 (Charlie) 5 | 2 (Bob) 6 | 1 (Alice) If I run the following query: SELECT DISTINCT user FROM event ORDER BY "order" DESC; will it be guaranteed that I get the results in the following order? 1 (Alice) 2 (Bob) 3 (Charlie) If the three last rows of event are selected, I know this is the order I get, because it would be

What is a valid pointer in gcc linux x86-64 C++?

余生长醉 提交于 2019-12-24 19:32:47
问题 I am programming C++ using gcc on an obscure system called linux x86-64. I was hoping that may be there are a few folks out there who have used this same, specific system (and might also be able to help me understand what is a valid pointer on this system). I do not care to access the location pointed to by the pointer, just want to calculate it via pointer arithmetic. According to section 3.9.2 of the standard: A valid value of an object pointer type represents either the address of a byte

C and C++ operand resolution order

只谈情不闲聊 提交于 2019-12-24 17:57:40
问题 Many times I see (and sometimes write) code similar to this example: int a=0, b=2; if( a && (b=func())!=0 ) { //... The question is: does the standard guarantee these statements? b will be not touched (and remain value 2 ) func() will not be called And vice-versa, if we write if( func()!=0 && a ) - does the standard guarantee func() will be called? I'm interested in the particular standard paragraph defining this is legitimate. UPD: my typo, changed from int a=1 to int a=0 回答1: To the exact

Adjacent character and string literal tokens

牧云@^-^@ 提交于 2019-12-24 16:28:27
问题 It's a familiar fact that in C you can write "a" "b" and get "ab" . This is discussed in the C11 standard: In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed string literal tokens are concatenated into a single multibyte character sequence. The phrase "character and..." would seem to suggest you can get the same results by writing 'a' "b" , but I've never come across that usage and GCC and the Microsoft compiler