unions

cl x64: unsigned long outside / inside union: error C2099: initializer is not a constant / NO error

老子叫甜甜 提交于 2021-01-27 19:50:17
问题 Case 1. File: test1.c : unsigned long val = (unsigned long)&"test"; int main() { return 0; } Compiler invocation: cl test1.c /c Results: Microsoft (R) C/C++ Optimizing Compiler Version 19.25.28611 for x64 Copyright (C) Microsoft Corporation. All rights reserved. test1.c test1.c(1): warning C4311: 'type cast': pointer truncation from 'char (*)[5]' to 'unsigned long' test1.c(1): error C2099: initializer is not a constant Case 2. File: test2.c : union { unsigned long val; } val = { (unsigned

cl x64: unsigned long outside / inside union: error C2099: initializer is not a constant / NO error

天涯浪子 提交于 2021-01-27 19:20:26
问题 Case 1. File: test1.c : unsigned long val = (unsigned long)&"test"; int main() { return 0; } Compiler invocation: cl test1.c /c Results: Microsoft (R) C/C++ Optimizing Compiler Version 19.25.28611 for x64 Copyright (C) Microsoft Corporation. All rights reserved. test1.c test1.c(1): warning C4311: 'type cast': pointer truncation from 'char (*)[5]' to 'unsigned long' test1.c(1): error C2099: initializer is not a constant Case 2. File: test2.c : union { unsigned long val; } val = { (unsigned

Allocating memory for unions and difference between union pointer and union of pointers

≡放荡痞女 提交于 2021-01-27 14:30:44
问题 Since my question here couldn't be confidently answered, I ask here again in hope that someone knows for sure: Is there any difference (besides syntactical) between a pointer to a union and a union that contains pointers to its elements? The generated assembly in this example is identical. As long as I'm never accessing the other members, is it allowed to allocate memory for only one of the members (which isn't the largest)? Regarding the 2nd question, 6.5.2.1 of the C89 draft says: The size

Implicitly defined constructor deleted due to variant member, N3690/N4140 vs N4659/N4727

纵饮孤独 提交于 2021-01-27 12:12:32
问题 My story starts off the same as this person's here: Unions in C++11: default constructor seems to be deleted The resolution here (now about three years old) is a bit unsatisfactory, because the "Digging into the standard" that the author did ended up with concluding that the behavior was as described in the standard, but unfortunately the quote is from a Note , and those are supposed to be non-normative (I've been told). Anyways, there's a link to an old bug report on gcc that is claimed to

Rationale behind active members of unions

最后都变了- 提交于 2021-01-27 07:20:49
问题 C++'s unions are more restrictive than those of C, because they introduce the concept of an "active member" (the one last assigned to) as the only one safe to access. The way I see it, this behavior of unions is a net negative. Can someone please explain what is gained by having this restriction? 回答1: Short answer In C, the union is only a question of how to interpret the data that is stored at a given location. The data is passive. In C++, unions can have members of different classes. And

Rationale behind active members of unions

无人久伴 提交于 2021-01-27 07:20:24
问题 C++'s unions are more restrictive than those of C, because they introduce the concept of an "active member" (the one last assigned to) as the only one safe to access. The way I see it, this behavior of unions is a net negative. Can someone please explain what is gained by having this restriction? 回答1: Short answer In C, the union is only a question of how to interpret the data that is stored at a given location. The data is passive. In C++, unions can have members of different classes. And

Typescript: create union instead intersection when merging optional with required type

被刻印的时光 ゝ 提交于 2020-12-31 05:49:17
问题 When optional and required property are merged via intersection, required wins type A = { who: string } type B = { who?: string } // $ExpectType {who:string} type R = A & B This may lead to runtime errors, when for instance, dealing with default params pattern within a function type Params = { who: string greeting: string } const defaults: Params = { greeting: 'Hello', who: 'Johny 5', } function greeting(params: Partial<Params>){ // $ExpectType Params const merged = {...defaults, ...params}

Typescript: create union instead intersection when merging optional with required type

不打扰是莪最后的温柔 提交于 2020-12-31 05:48:29
问题 When optional and required property are merged via intersection, required wins type A = { who: string } type B = { who?: string } // $ExpectType {who:string} type R = A & B This may lead to runtime errors, when for instance, dealing with default params pattern within a function type Params = { who: string greeting: string } const defaults: Params = { greeting: 'Hello', who: 'Johny 5', } function greeting(params: Partial<Params>){ // $ExpectType Params const merged = {...defaults, ...params}

Typescript: create union instead intersection when merging optional with required type

心不动则不痛 提交于 2020-12-31 05:48:27
问题 When optional and required property are merged via intersection, required wins type A = { who: string } type B = { who?: string } // $ExpectType {who:string} type R = A & B This may lead to runtime errors, when for instance, dealing with default params pattern within a function type Params = { who: string greeting: string } const defaults: Params = { greeting: 'Hello', who: 'Johny 5', } function greeting(params: Partial<Params>){ // $ExpectType Params const merged = {...defaults, ...params}

Typescript: create union instead intersection when merging optional with required type

╄→гoц情女王★ 提交于 2020-12-31 05:47:43
问题 When optional and required property are merged via intersection, required wins type A = { who: string } type B = { who?: string } // $ExpectType {who:string} type R = A & B This may lead to runtime errors, when for instance, dealing with default params pattern within a function type Params = { who: string greeting: string } const defaults: Params = { greeting: 'Hello', who: 'Johny 5', } function greeting(params: Partial<Params>){ // $ExpectType Params const merged = {...defaults, ...params}