language-lawyer

=+ Python operator is syntactically correct

早过忘川 提交于 2019-12-19 05:23:24
问题 I accidentally wrote: total_acc =+ accuracy instead of: total_acc += accuracy I searched the net and could not find anything. So what happened, why does Python think I mean what I am typing? Computers trust us too much. :) 回答1: This is the same as if you were to do like total_acc = -accuracy , except positive instead of negative. It basically is the same as total_acc = accuracy though, as adding a + before a value does not change it. This is called an unary operator as there is only one

T* versus char* pointer arithmetic

喜你入骨 提交于 2019-12-19 05:23:07
问题 Assume we have an array that contains N elements of type T. T a[N]; According to the C++14 Standard , under which conditions do we have a guarantee that (char*)(void*)&a[0] + n*sizeof(T) == (char*)(void*)&a[n], (0<=n<N) ? While this is true for many types and implementations, the standard mentions it in a footnote, and in an ambiguous way: §5.7.6, footnote 85) Another way to approach pointer arithmetic ... There is little indication that this other way was thought of being equivalent to the

Is there any situation in which an object's storage might change during its lifetime?

谁说胖子不能爱 提交于 2019-12-19 05:14:07
问题 I've always assumed that an object begins and ends its lifetime in the same memory location, but I've recently come across a scenario where I need to be sure. Specifically, I'm looking for a guarantee from the standard that no matter what optimizations the compiler performs the address an object is constructed at is the same one that it will have its destructor called from... and that its destructor is, indeed, guaranteed to be called from that location unless the program is terminating. I've

Can class members be defined outside the namespace in which they are declared?

余生颓废 提交于 2019-12-19 05:13:19
问题 Sometimes I find code like the following (actually some class-wizards create such code): // C.h namespace NS { class C { void f(); }; } and in the implementation file: // C.cpp #include "C.h" using namespace NS; void C::f() { //... } All the compilers I tried accept that kind of code (gcc, clang, msvc, compileonline.com). What makes me feel uncomfortable is the using namespace NS; . From my point of view C::f() lives in the global namespace in an environment that has unqualified access to

Inside of a class, why `auto b() -> decltype(a()) {}` works, but `decltype(a()) b() {}` does not?

廉价感情. 提交于 2019-12-19 05:13:10
问题 Consider following code: (Ideone) struct S { int a() {return 0;} decltype(a()) b() {return 1;} }; It gives me following error: error: cannot call member function 'int S::a()' without object On the other hand, this code compiles fine: (Ideone) struct S { int a() {return 0;} auto b() -> decltype(a()) {return 1;} }; Why one example works, but another fails to compile? Is compiler behavior fully correct in both examples? If compiler is correct, then why the standard mandates such strange behavior

How does template argument deduction work when an overloaded function is involved as an argument?

孤人 提交于 2019-12-19 05:12:57
问题 This is the more sophisticated question mentioned in How does overload resolution work when an argument is an overloaded function? Below code compiles without any problem: void foo() {} void foo(int) {} void foo(double) {} void foo(int, double) {} // Uncommenting below line break compilation //template<class T> void foo(T) {} template<class X, class Y> void bar(void (*f)(X, Y)) { f(X(), Y()); } int main() { bar(foo); } It doesn't appear a challenging task for template argument deduction -

Is sizeof(int()) a legal expression?

限于喜欢 提交于 2019-12-19 05:06:43
问题 This question is inspired by Is sizeof(void()) a legal expression? but with an important difference as explained below. The expression in question is: sizeof( int() ) In the C++ grammar there appears: unary-expression: sizeof unary-expression sizeof ( type-id ) however, ( int() ) can match both of these cases with different meanings: As a unary-expression , it is a value-initialized int prvalue, surrounded in redundant parentheses As a type-id , it is the type of a function with no parameters

Does reinterpret_casting std::aligned_storage* to T* without std::launder violate strict-aliasing rules? [duplicate]

只愿长相守 提交于 2019-12-19 04:13:13
问题 This question already has answers here : Does this really break strict-aliasing rules? (2 answers) Closed 2 years ago . The following example comes from std::aligned_storage page of cppreference.com: #include <iostream> #include <type_traits> #include <string> template<class T, std::size_t N> class static_vector { // properly aligned uninitialized storage for N T's typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N]; std::size_t m_size = 0; public: // Create an object in

Is it defined to provide an empty range to C++ standard algorithms?

孤街浪徒 提交于 2019-12-19 04:10:33
问题 Following on from my previous question, can we prove that the standard allows us to pass an empty range to a standard algorithm? Paragraph 24.1/7 defines an "empty range" as the range [i,i) (where i is valid), and i would appear to be "reachable" from itself, but I'm not sure that this qualifies as a proof. In particular, we run into trouble when looking at the sorting functions. For example, std::sort : Complexity: O(N log(N)) (where N == last - first ) comparisons Since log(0) is generally

Is it defined to provide an empty range to C++ standard algorithms?

北慕城南 提交于 2019-12-19 04:10:07
问题 Following on from my previous question, can we prove that the standard allows us to pass an empty range to a standard algorithm? Paragraph 24.1/7 defines an "empty range" as the range [i,i) (where i is valid), and i would appear to be "reachable" from itself, but I'm not sure that this qualifies as a proof. In particular, we run into trouble when looking at the sorting functions. For example, std::sort : Complexity: O(N log(N)) (where N == last - first ) comparisons Since log(0) is generally