language-lawyer

Is placement new legally required for putting an int into a char array?

本秂侑毒 提交于 2020-01-11 15:28:29
问题 There seems to be some agreement that you can't willy nilly point (an int*) into a char array because of the C++ aliasing rules. From this other question -- Generic char[] based storage and avoiding strict-aliasing related UB -- it seems that it is allowed to (re-)use storage through placement new. alignas(int) char buf[sizeof(int)]; void f() { // turn the memory into an int: (??) from the POV of the abstract machine! ::new (buf) int; // is this strictly required? (aside: it's obviously a no

Do C and C++ standards imply that a special value in the address space must exist solely to represent the value of null pointers?

a 夏天 提交于 2020-01-11 14:26:31
问题 Following discussion from this question about null pointers in C and C++, I'd like to have the ending question separated here. If it can be inferred from C and C++ standards (answers can target both standards) that dereferencing a pointer variable whose value is equal to the nullptr (or (void *)0 ) value is undefined behavior, does it imply that these languages require that a special value in the address space is dead , meaning that it's unusable except for the role of representing nullptr ?

Is pointer arithmetic on allocated storage UB?

前提是你 提交于 2020-01-11 11:35:11
问题 Let's say I want to implement std::vector without invoking any undefined behavior (UB). Is the code below invokes UB: struct X{int i;}; int main(){ auto p = static_cast<X*>(::operator new(sizeof(X)*2)); new(p) X{}; new(p+1) X{};// p+1 UB? } Folowing a selection of quote from the standard that may help: [basic.stc.dynamic.allocation] The pointer returned (by an allocation function) shall be suitably aligned so that it can be converted to a pointer to any suitable complete object type (21.6.2.1

Value stored when istream read fails

巧了我就是萌 提交于 2020-01-11 11:10:08
问题 Sample code: #include <iostream> int main() { int x = 5; std::cin >> x; std::cout << x << '\n'; } On one particular implementation the following behaviour occurs: Input: 6 ; output 6 Input: a ; output: 0 Input: (end-of-file); output 5 Input: (whitespace followed by end-of-file); output 5 So, on failure, the cin >> x is assigning 0 to x if it was a failure to convert text to int; but it is not assigning 0 if the failure was due to end-of-file. Is this correct behaviour? If not, what is the

Is `this` allowed inside a noexcept specification?

独自空忆成欢 提交于 2020-01-11 08:17:22
问题 I have some code which requires me to use *this , but I want it to be noexcept friendly: struct foo; // Would actually be something with conditional noexcept void do_something(foo&); struct foo { void fn() noexcept(noexcept(::do_something(*this))) { ::do_something(*this); } }; However, gcc rejects this: <source>:7:43: error: invalid use of 'this' at top level noexcept(noexcept(::do_something(*this))) If I just access a member, gcc is fine: void do_something(int); struct bar { int x; void fn()

Is `this` allowed inside a noexcept specification?

不打扰是莪最后的温柔 提交于 2020-01-11 08:17:05
问题 I have some code which requires me to use *this , but I want it to be noexcept friendly: struct foo; // Would actually be something with conditional noexcept void do_something(foo&); struct foo { void fn() noexcept(noexcept(::do_something(*this))) { ::do_something(*this); } }; However, gcc rejects this: <source>:7:43: error: invalid use of 'this' at top level noexcept(noexcept(::do_something(*this))) If I just access a member, gcc is fine: void do_something(int); struct bar { int x; void fn()

Lifetime of object which has vacuous initialization

筅森魡賤 提交于 2020-01-11 06:58:17
问题 Current draft standard says (previous standards have similar wording) in [basic.life/1]: The lifetime of an object or reference is a runtime property of the object or reference. An object is said to have non-vacuous initialization if it is of a class or aggregate type and it or one of its subobjects is initialized by a constructor other than a trivial default constructor. [ Note: Initialization by a trivial copy/move constructor is non-vacuous initialization. — end note ] The lifetime of an

Do 'a' and '0' always have positive values even if char is signed?

北慕城南 提交于 2020-01-11 04:33:06
问题 Depending on the environment and compiler settings, the type char can be signed or unsigned by default, which means the range of values for single character constants on 8-bit 2s complement systems can be either -128..127 or 0..255 . In the ubiquitous ASCII character set, its ISO-8859-X extensions or the UTF-8 encoding, upper- and lowercase letters as well as digits have values below 127. But such is not the case with the EBCDIC character set: 'A' is 0xC1, 'a' is 0x81 and '1' is 0xF1. Since

In the CSS Visual Formatting Model, what does “the flow of an element” mean?

依然范特西╮ 提交于 2020-01-11 03:41:05
问题 In CSS2 Section 9.3: Positioning schemes: An element is called out of flow if it is floated, absolutely positioned, or is the root element. An element is called in-flow if it is not out-of-flow. The flow of an element A is the set consisting of A and all in-flow elements whose nearest out-of-flow ancestor is A. I can understand what out of flow and in-flow means, but I don't understand what "nearest out-of-flow ancestor" in the last sentence means. Can anyone provide a simple example? 回答1: An

Why is throwing a checked exception type allowed in this case?

心不动则不痛 提交于 2020-01-10 17:33:09
问题 I noticed by accident that this throw statement (extracted from some more complex code) compiles: void foo() { try { } catch (Throwable t) { throw t; } } For a brief but happy moment I thought that checked exceptions had finally decided to just die already, but it still gets uppity at this: void foo() { try { } catch (Throwable t) { Throwable t1 = t; throw t1; } } The try block doesn't have to be empty; it seems it can have code so long as that code doesn't throw a checked exception. That