How do boost::variant and boost::any work?

If you read the boost::any documentation they provide the source for the idea: http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf It’s basic information hiding, an essential C++ skill to have. Learn it! Since the highest voted answer here is totally incorrect, and I have my doubts that people will actually go look at the source to verify that fact, here’s a … Read more

C++ union in C#

You can use explicit field layouts for that: [StructLayout(LayoutKind.Explicit)] public struct SampleUnion { [FieldOffset(0)] public float bar; [FieldOffset(4)] public int killroy; [FieldOffset(4)] public float fubar; } Untested. The idea is that two variables have the same position in your struct. You can of course only use one of them. More informations about unions in struct … Read more

What’s the purpose of using a union with only one member?

Because tx_side is a union, tx_side() doesn’t automatically initialize/construct a, and ~tx_side() doesn’t automatically destruct it. This allows a fine-grained control over the lifetime of a and pending_fifo, via placement-new and manual destructor calls (a poor man’s std::optional). Here’s an example: #include <iostream> struct A { A() {std::cout << “A()\n”;} ~A() {std::cout << “~A()\n”;} }; … Read more

Why does C++ disallow anonymous structs?

As others have pointed out anonymous unions are permitted in standard C++, but anonymous structs are not. The reason for this is that C supports anonymous unions but not anonymous structs*, so C++ supports the former for compatibility but not the latter because it’s not needed for compatibility. Furthermore, there’s not much use to anonymous … Read more

Unions and type-punning

To re-iterate, type-punning through unions is perfectly fine in C (but not in C++). In contrast, using pointer casts to do so violates C99 strict aliasing and is problematic because different types may have different alignment requirements and you could raise a SIGBUS if you do it wrong. With unions, this is never a problem. … Read more

When would anyone use a union? Is it a remnant from the C-only days?

Unions are usually used with the company of a discriminator: a variable indicating which of the fields of the union is valid. For example, let’s say you want to create your own Variant type: struct my_variant_t { int type; union { char char_value; short short_value; int int_value; long long_value; float float_value; double double_value; void* ptr_value; … Read more

Typescript has unions, so are enums redundant?

With the recent versions of TypeScript, it is easy to declare iterable union types. Therefore, you should prefer union types to enums. How to declare iterable union types const permissions = [‘read’, ‘write’, ‘execute’] as const; type Permission = typeof permissions[number]; // ‘read’ | ‘write’ | ‘execute’ // you can iterate over permissions for (const … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)