standards

Why can I invoke == with a defaulted <=> but not a user-provided one?

☆樱花仙子☆ 提交于 2020-08-19 04:25:08
问题 #include <compare> struct A { int n; auto operator <=>(const A&) const noexcept = default; }; struct B { int n; auto operator <=>(const B& rhs) const noexcept { return n <=> rhs.n; } }; int main() { A{} == A{}; // ok B{} == B{}; // error: invalid operands to binary expression } compiled with clang-10 as clang -std=c++20 -stdlib=libc++ main.cpp Why does A{} == A{} work but not B{} == B{} ? 回答1: In the original design of the spaceship operator, == is allowed to call <=> , but this is later

Will C++ first gets converted to assembly [duplicate]

吃可爱长大的小学妹 提交于 2020-08-05 10:18:10
问题 This question already has answers here : Does the C++ code compile to assembly codes? (3 answers) Does a compiler always produce an assembly code? (4 answers) Closed 7 years ago . I have confusion. I am C++ developer and heard many times that my source code will first gets converted to assembly and then assembly will get converted to machine code. But in one of the video tutorial of assembly language, instructor clearly said, C/C++ code directly gets convert to machine code. (Of course there

In the C++ standard does well-formed means that the code compiles?

随声附和 提交于 2020-07-09 12:12:19
问题 The C++ standards defines well-formed programs as C ++ program constructed according to the syntax rules, diagnosable semantic rules, and the one-definition rule I am wondering if all well-formed program compile or not (if it is not the case, what types of error make the difference between a well-formed program and a compilable problem). For example would a program containing ambiguity errors considered as well-formed? 回答1: A well-formed program can have undefined behaviour. It's in a note,

Empty return in non-void function, is undefined behaviour?

狂风中的少年 提交于 2020-06-27 07:17:20
问题 After reading answers about the topic of control reaches end of non-void functions I didn't see any answer adressing specifically the the case of exiting a non-void function with an empty return statement: int return_integer() { return; } // empty return in non-void function What I've found so far in the C standard is: 6.8.6.4 The return statement Constraints A return statement with an expression shall not appear in a function whose return type is void . A return statement without an

Why does a function prototype with an empty argument list conflicts with one that has a char argument?

大兔子大兔子 提交于 2020-05-29 04:05:44
问题 With the code below, both clang and gcc called with -std=c11 complain about conflicting types for foo. int foo(); int main(void) { return foo(0); } int foo(char a) { return a; } According to the answer in https://stackoverflow.com/a/13950800/1078224, in (older?) C standards the type int has been assumed when no variable type was given. However, the C11 standard draft (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), section 6.7.6.3, $14 says that The empty list in a function

Why does the C++ standard specifically grant leeway regarding memory layout of class data members with different access specifiers?

[亡魂溺海] 提交于 2020-05-14 19:11:29
问题 The C++11 standard mandates an ordering in memory for the non-static data members of a class but then specifically carves out an exemption for members with different access specifiers. Why though? § 9.2.13 Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (Clause 11). Implementation

Why does the C++ standard specifically grant leeway regarding memory layout of class data members with different access specifiers?

冷暖自知 提交于 2020-05-14 19:09:57
问题 The C++11 standard mandates an ordering in memory for the non-static data members of a class but then specifically carves out an exemption for members with different access specifiers. Why though? § 9.2.13 Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (Clause 11). Implementation

C null pointer arithmetic

旧街凉风 提交于 2020-03-16 06:35:38
问题 I noticed this warning from Clang: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] In details, it is this code which triggers this warning: uint8_t *end = ((uint8_t*)0) + sizeof(uint8_t) * count; Why would arithmetic on a null pointer be forbidden when doing the same on a non-null pointer obtained from an integer different than zero does not trigger any warning ? And more importantly, does the C standard explicitly forbid null

Is there standard way of making multiple API calls combined into one HTTP request?

橙三吉。 提交于 2020-02-28 12:59:39
问题 While designing rest API's I time to time have challenge to deal with batch operations (e.g. delete or update many entities at once) to reduce overhead of many tcp client connections. And in particular situation problem usually solves by adding custom api method for specific operation (e.g. POST /files/batchDelete which accepts ids at request body) which doesn't look pretty from point of view of rest api design principles but do the job. But for me general solution for the problem still

What is the rationale for the way C++ handles uniform initialization with initialization lists?

旧巷老猫 提交于 2020-02-24 14:09:05
问题 C++'s uniform initialization syntax fixes the most vexing parse. Yay. But then it introduces confusion when dealing with initialization lists. Boo. The existing behavior is that: std::vector<int> the_vec{4}; will invoke std::vector(std::initializer_list<T> init) instead of std::vector(size_type count); . What is the rationale for this decision? Especially since the language committee was inventing new syntax, it seems to me the other possible designs could have been: Require initialization