language-lawyer

Default behavior of copy module on user-defined classes

三世轮回 提交于 2020-02-21 13:20:31
问题 When copy.copy or copy.deepcopy is called on an instance of a user-defined class that does not have a __copy__ or __deepcopy__ method, what does Python guarantee will happen? The official docs are disturbingly non-explicit on this matter. Will the function always just return a new instance of the same class with a shallow/deep copy of the original object's __dict__ (or whatever the equivalent is when __slots__ are involved)? Can the behavior differ between CPython, PyPy, etc.? Does the

Default behavior of copy module on user-defined classes

青春壹個敷衍的年華 提交于 2020-02-21 13:19:30
问题 When copy.copy or copy.deepcopy is called on an instance of a user-defined class that does not have a __copy__ or __deepcopy__ method, what does Python guarantee will happen? The official docs are disturbingly non-explicit on this matter. Will the function always just return a new instance of the same class with a shallow/deep copy of the original object's __dict__ (or whatever the equivalent is when __slots__ are involved)? Can the behavior differ between CPython, PyPy, etc.? Does the

Default behavior of copy module on user-defined classes

走远了吗. 提交于 2020-02-21 13:17:31
问题 When copy.copy or copy.deepcopy is called on an instance of a user-defined class that does not have a __copy__ or __deepcopy__ method, what does Python guarantee will happen? The official docs are disturbingly non-explicit on this matter. Will the function always just return a new instance of the same class with a shallow/deep copy of the original object's __dict__ (or whatever the equivalent is when __slots__ are involved)? Can the behavior differ between CPython, PyPy, etc.? Does the

Why are CSS named grid areas not in quotes?

隐身守侯 提交于 2020-02-21 06:09:15
问题 According to the spec, the value for grid-area is grid-line, which further uses custom-ident. Then MDN states that identifies cannot be put between quotes, which will make it a string value, which by itself is reasonable. But adding these all up, named grid areas must be accessed via an ID without quotes. See the comparison in the example below: .grid { display:grid; grid: "a b" 1fr "c d" 1fr / 1fr 1fr; } .foo { /* this works just fine, putting it to area b in upper right corner */ grid-area:

Why are CSS named grid areas not in quotes?

柔情痞子 提交于 2020-02-21 06:08:30
问题 According to the spec, the value for grid-area is grid-line, which further uses custom-ident. Then MDN states that identifies cannot be put between quotes, which will make it a string value, which by itself is reasonable. But adding these all up, named grid areas must be accessed via an ID without quotes. See the comparison in the example below: .grid { display:grid; grid: "a b" 1fr "c d" 1fr / 1fr 1fr; } .foo { /* this works just fine, putting it to area b in upper right corner */ grid-area:

Is it allowed to call a non-static member function in a default member initializer?

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-02 11:27:12
问题 Consider this class: #include <iostream> struct foo { int a = 42; int b = bar(); int bar() { return a; } }; int main(){ foo f; std::cout << f.a << " " << f.b; } It prints the expected 42 42 . Is it allowed by the standard to call a member function in a default member initializer? The following I would expect to be undefined: struct broken { int a = bar(); int b = 42; int bar() { return b; } }; Unfortunately it does compile without warnings. 回答1: As you found, this is legal, but brittle and

Is jumping over a variable initialization ill-formed or does it cause undefined behaviour?

最后都变了- 提交于 2020-02-01 15:32:52
问题 Consider this code: void foo() { goto bar; int x = 0; bar: ; } GCC and Clang reject it, because the jump to bar: bypasses variable initialization. MSVC doesn't complain at all (except using x after bar: causes a warning). We can do a similar thing with a switch : void foo() { switch (0) { int x = 0; case 0: ; } } Now all three compilers emit errors. Are those snippets ill-formed? Or do they cause UB? I used to think that both were ill-formed, but I can't find the revelant parts of the

Is jumping over a variable initialization ill-formed or does it cause undefined behaviour?

痴心易碎 提交于 2020-02-01 15:31:24
问题 Consider this code: void foo() { goto bar; int x = 0; bar: ; } GCC and Clang reject it, because the jump to bar: bypasses variable initialization. MSVC doesn't complain at all (except using x after bar: causes a warning). We can do a similar thing with a switch : void foo() { switch (0) { int x = 0; case 0: ; } } Now all three compilers emit errors. Are those snippets ill-formed? Or do they cause UB? I used to think that both were ill-formed, but I can't find the revelant parts of the

Why isn't argument deduction allowed in function return type?

青春壹個敷衍的年華 提交于 2020-02-01 04:12:05
问题 The most obvious answer could be - because the standard says so . That's fine, but I'm wrapping my head around it to understand the reasons behind this choice. Consider the following example: template<typename T> struct S { S(T) {} }; S f() { return 0; } int main() { auto s = f(); (void)s; } It fails to compile with errors like: error: use of class template 'S' requires template arguments; argument deduction not allowed in function return type Quite easy to fix, it isn't a problem, something

Why is the code “foo::foo::foo::foob” compiling? [duplicate]

谁都会走 提交于 2020-02-01 00:42:16
问题 This question already has an answer here : Why is there an injected class name? (1 answer) Closed 2 years ago . A co-worker accidentally wrote code like this: struct foo { foo() : baz(foobar) {} enum bar {foobar, fbar, foob}; bar baz; }; void f() { for( auto x : { foo::foobar, foo::fbar, foo:: foo:: foo::foob } ); // ... } GCC 5.1.0 compiles this. What's the rule that makes this compile? 回答1: The injected-class-name is used here, the name of the class within its own definition acts as a