language-lawyer

Is the order of execution of operations in Javascript guaranteed to be the same at all times?

淺唱寂寞╮ 提交于 2021-02-04 18:17:28
问题 When I do something like this: var x = 5; console.log( x + (x += 10) ); //(B) LOGS 10, X == 20 console.log( (x += 10) + x ); //(A) LOGS 0, X == 30 The difference in the returned value between (A) and (B) is explained by the value of x at the time it becomes evaluated. I figure that backstage something like this should happen: TIME ----> (A) (x = 5) + (x += 10 = 15) = 20 (B) (x += 10 == 15) + (x == 15) = 30 But this only holds true if and only if x is evaluated in the same left-to-right order

Why C++'s <vector> templated class doesn't break one definition rule?

不打扰是莪最后的温柔 提交于 2021-02-04 17:18:22
问题 Maybe its lame question, But I don't get it! If I include <string> or <vector> in multiple translation units (different .cpp) why it doesn't break the ODR? As far as I know each .cpp is compiled differently so vector's methods code will be generated for each object file separately, right? So linker should detect it and complain. Even If it won't (I suspect it's special case for templates) will it be using one code or different set of cloned code in each unit, when I link all together??? 回答1:

Template parameters of function type with auto return type arguments of previous template parameter types

一笑奈何 提交于 2021-02-04 15:39:19
问题 I have a template with two parameters: the first is a type, and the second is a function pointer with an argument whose type is the first template parameter. This MCVE works: void returnsVoid(int x) { } template <typename T, void (*Func)(T)> struct foo { void bar(T t) { Func(t); } }; int main(int, char *[]) { foo<int, returnsVoid> a; // ok } However, when I change the return type of the second template parameter to auto (as explained in this related question), I get an error: void returnsVoid

Template parameters of function type with auto return type arguments of previous template parameter types

心不动则不痛 提交于 2021-02-04 15:39:19
问题 I have a template with two parameters: the first is a type, and the second is a function pointer with an argument whose type is the first template parameter. This MCVE works: void returnsVoid(int x) { } template <typename T, void (*Func)(T)> struct foo { void bar(T t) { Func(t); } }; int main(int, char *[]) { foo<int, returnsVoid> a; // ok } However, when I change the return type of the second template parameter to auto (as explained in this related question), I get an error: void returnsVoid

Does C check if a pointer is out-of-bound without the pointer being dereferenced?

半城伤御伤魂 提交于 2021-02-04 14:21:09
问题 I had this argument with some people saying that C out-of-bound pointers cause undefined behavior even if they're not being dereferenced. example: int a; int *p = &a; p = p - 1; the third line here will cause undefined behavior even if p is never dereferenced ( *p is never used). In my opinion, it sounds illogical that C would check if a pointer is out-of-bound without the pointer being used (it's like someone would inspect people on the street to see if they're carrying guns in case they

why a constexpr expression of literal class type with a pointer subobject can't be a non-type template argument

拟墨画扇 提交于 2021-02-04 06:32:38
问题 After looking at the post about non-type template argument,I have a confusion for an example in that post,I cite the example here: struct VariableLengthString { const char *data_ = nullptr; constexpr VariableLengthString(const char *p) : data_(p) {} auto operator<=>(const VariableLengthString&) const = default; }; template<VariableLengthString S> int bar() { static int i = 0; return ++i; } int main() { int x = bar<"hello">(); // ERROR } The post says "the relevant wording is [temp.arg.nontype

why a constexpr expression of literal class type with a pointer subobject can't be a non-type template argument

我的梦境 提交于 2021-02-04 06:30:26
问题 After looking at the post about non-type template argument,I have a confusion for an example in that post,I cite the example here: struct VariableLengthString { const char *data_ = nullptr; constexpr VariableLengthString(const char *p) : data_(p) {} auto operator<=>(const VariableLengthString&) const = default; }; template<VariableLengthString S> int bar() { static int i = 0; return ++i; } int main() { int x = bar<"hello">(); // ERROR } The post says "the relevant wording is [temp.arg.nontype

Why does the size of enum only correspond to the size of a single value?

徘徊边缘 提交于 2021-01-29 20:31:12
问题 This post does not have an answer to my question. Consider this: enum seq {VAL1, VAL2 = 1000000000, VAL3 = UINT_MAX}; int main(void) { printf("%lu\n", sizeof(enum seq)); } Here UINT_MAX is the max value for a uint32_t (4 billion or something) Why is the size of the entire enum type appears to be only 4 bytes? This is only enough to store a single integer value. 回答1: I think maybe I'm starting to understand your question. In your example program, the numbers 0 , 1000000000 and UINT_MAX do not

int shown as to take 8 bytes [duplicate]

一笑奈何 提交于 2021-01-29 18:13:34
问题 This question already has answers here : Why isn't sizeof for a struct equal to the sum of sizeof of each member? (12 answers) Closed 1 year ago . I have taken code from https://www.geeksforgeeks.org/the-offsetof-macro/. I ran the code in ide provided in gfg itself. I have edited code a bit , sizeof(int) is showing 4 but it is shown to take 8 bytes in struct through offset #include <stdio.h> #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) typedef struct PodTag { int i;

How is P0522R0 breaking code?

混江龙づ霸主 提交于 2021-01-29 07:11:57
问题 Today I was reading the C++17 support page of clang. I've notice something odd. The feature Matching template template parameters to compatible arguments (P0522R0) is marked as partial, because it must be activate through a switch. Their note says: Despite being the the resolution to a Defect Report, this feature is disabled by default in all language versions, and can be enabled explicitly with the flag -frelaxed-template-template-args in Clang 4. The change to the standard lacks a