language-lawyer

What exactly is a translation unit in C

点点圈 提交于 2019-12-30 05:52:05
问题 The commonly used definition of a translation unit is what comes after preprocessing (header files inclusions, macros, etc along with the source file). This definition is reasonably clear and the C standard, 5.1.1.1, C11, says: A C program need not all be translated at the same time. The text of the program is kept in units called source files, (or preprocessing files) in this International Standard. A source file together with all the headers and source files included via the preprocessing

Should operators be declared as non-member non-template friends

跟風遠走 提交于 2019-12-30 05:45:45
问题 Consider this question, which is about the following code not compiling: std::vector<int> a, b; std::cout << (std::ref(a) < std::ref(b)); It doesn't compile because the vector comparison operators for vector are non-member function templates, and implicit conversions aren't allowed to be considered. However, if the operators were instead written as non-member non-template, friend functions: template <class T, class Allocator = std::allocator<T>> class vector { // ... friend bool operator<

Can an lvalue reference non-type template parameter be inferred?

不羁岁月 提交于 2019-12-30 04:10:05
问题 I have the following code, which I cannot get to work: struct foo {}; foo foo1 = {}; template <foo& F> class FooClass {}; template <foo& F> void foobar(FooClass<F> arg) { } int main() { FooClass<foo1> f; foobar(f); } The error is: main.cpp:14:5: error: no matching function for call to 'foobar' note: candidate template ignored: substitution failure : deduced non-type template argument does not have the same type as the its corresponding template parameter ('foo' vs 'foo &') Is it at all

Can an XOR linked list be implemented in C++ without causing undefined behavior?

纵然是瞬间 提交于 2019-12-30 03:56:07
问题 An XOR linked list is a modified version of a normal doubly-linked list in which each node stores just one "pointer" instead of two. That "pointer" is composed of the XOR of the next and previous pointers. To traverse the list, two pointers are needed - one to the current node and one to the next or previous node. To traverse forward, the previous node's address is XORed with the "pointer" stored in the current node, revealing the true "next" pointer. The C++ standard causes a bunch of

Lvalues which do not designate objects in C++14

瘦欲@ 提交于 2019-12-30 03:25:13
问题 I'm using N3936 as a reference here (please correct this question if any of the C++14 text differs). Under 3.10 Lvalues and rvalues we have: Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. However the definition of lvalue reads: An lvalue [...] designates a function or an object. In 4.1 Lvalue-to-rvalue conversion the text appears: [...] In all other cases, the result of the conversion is determined according to the

Aliasing struct and array the C++ way

非 Y 不嫁゛ 提交于 2019-12-30 01:37:07
问题 This is a C++ followup for another question of mine In the old days of pre-ISO C, the following code would have surprised nobody: struct Point { double x; double y; double z; }; double dist(struct Point *p1, struct Point *p2) { double d2 = 0; double *coord1 = &p1->x; double *coord2 = &p2->x; int i; for (i=0; i<3; i++) { double d = coord2[i] - coord1[i]; // THE problem d2 += d * d; } return sqrt(d2); } Unfortunately, this problematic line uses pointer arithmetic ( p[i] being by definition *(p

Is memcpy of a trivially-copyable type construction or assignment?

青春壹個敷衍的年華 提交于 2019-12-30 00:51:09
问题 Let's say you have an object of type T and a suitably-aligned memory buffer alignas(T) unsigned char[sizeof(T)] . If you use std::memcpy to copy from the object of type T to the unsigned char array, is that considered copy construction or copy-assignment? If a type is trivially-copyable but not standard-layout, it is conceivable that a class such as this: struct Meow { int x; protected: // different access-specifier means not standard-layout int y; }; could be implemented like this, because

Is memcpy of a trivially-copyable type construction or assignment?

家住魔仙堡 提交于 2019-12-30 00:51:09
问题 Let's say you have an object of type T and a suitably-aligned memory buffer alignas(T) unsigned char[sizeof(T)] . If you use std::memcpy to copy from the object of type T to the unsigned char array, is that considered copy construction or copy-assignment? If a type is trivially-copyable but not standard-layout, it is conceivable that a class such as this: struct Meow { int x; protected: // different access-specifier means not standard-layout int y; }; could be implemented like this, because

template with lambda as unique default parameter on each instantiation

旧城冷巷雨未停 提交于 2019-12-30 00:35:13
问题 I'm looking for a way to automatically make default template parameter be unique each time a template is instantiated. Since unnamed function objects created by lambda expressions have different types I thought of adopting them somehow. With recent changes to standard daft removing "A lambda-expression shall not appear in ... a template-argument" restriction (see Wording for lambdas in unevaluated contexts) it seemed like a good idea. So I wrote the following kinda working snippet that

Why is this an invalid assignment left hand side?

故事扮演 提交于 2019-12-30 00:13:07
问题 Why can I do the following operations: var b1, b2; b1 = b2 = true; document.write(b1," ", b2); document.write("<br>"); b1 = !b2; document.write(b1," ", b2); document.write("<br>"); b1 = b2 = !true; document.write(b1," ", b2); Yet when I try the following operation I recieve a ReferenceError: invalid assignment left-hand side ? var b1, b2; b1 = !b2 = true; document.write(b1," ", b2); It's obvious that I can't do this, but I can't find an explanation as to why I can't. The MDN developer guide