Live data from Hacker News

Learning that you can use unions in C for grouping things into namespaces

utcc.utoronto.ca

101–110 of 150 posts

Re: Learning that you can use unions in C for grouping things into namespaces

#101

Earlier quoted context omitted.

I want to be clear about your meaning, because I don’t know if I’m reading your comment correctly. Are you referring explicitly to syntax based, preprocessor macros? Or does your comment extend to other metaprogramming techniques? I am inclined to think you mean the first considering the amount of emphasis on generic programming in D? Just curious.

I'm referring to both syntax based (AST) macros, and text based (preprocessor) macros. The latter, of course, are much worse. An example of the former is so-called "expression templates" in C++. I've seen them used to create a regular expression language using C++ expression templates. The author was quite proud of them, and indeed they were very clever. However nice the execution, the concept was terrible. There was…

> Anytime macros are used for metaprogramming, it's time to reach for a more powerful language.

> I'm referring to both syntax based (AST) macros ...

This surprises me greatly. Various lisps are among the most powerful languages I know of and a large part of the reason is macros coupled with their ability to execute arbitrary code at compile time (which itself uses additional macros, which in turn invoke more code, and so on). What's your take on this?

(Continuations are also pretty nice ...)

Re: Learning that you can use unions in C for grouping things into namespaces

#102
post #8

Earlier quoted context omitted.

You can use inline namespaces for versioning symbols. https://www.foonathan.net/2018/11/inline-namespaces/

First of all, C++ 11 may feel like thirty years ago, and certainly some of its proponents look thirty years older than they did at the time, but it was only ten years ago. C++ namespaces date to standardisation work (so after the 1985 C++ but before the 1995 standard C++) but they don't get this job done. Inline namespaces are a newer feature. Secondly this technique does something different. The C hack doesn't touch…

Ah, another of those threads, ok lets set the years straight.

Yes, inline namespaces were only introduced in C++11, about 10 years ago, now lets dive into article.

"Learning that you can use unions in C for grouping things into namespaces"

Grouping into namespaces, so when did C++ get said feature?

ANSI/ISO C++89 released to the world in September 1998, which makes around 23 years, or 24 years if we consider the release of C++ compilers already supporting it the year before, like Borland C++.

This C hack definitly does touch old code, as it requires the code to be written to take advantage of the technique and is also touched again, when changes to the structs are required.

And naturally recompilation.

With inline namespaces, assumign recompilation you can naturally also change which set of identifiers and type aliases are visibile by default.

Re: Learning that you can use unions in C for grouping things into namespaces

#103

Earlier quoted context omitted.

Accessing packed struct members works fine on x86, but will blow up at runtime or do weird things on platforms which don't support unaligned loads or stores. The correct way to access packed structs is through memcpy, just like you'd access any other potentially unaligned object.

For architectures where unaligned accesses are illegal, gcc will generate multiple load/store instructions when accessing packed struct fields by name. The main caveat to look out for is taking the address of a packed struct member and then dereferencing it.

Ah, right. Thanks for the correction.

Re: Learning that you can use unions in C for grouping things into namespaces

#104
post #61

Earlier quoted context omitted.

> I really don’t understand why they are not allowed. I don't, either. Such were in D from 2000 or so. I also don't understand why `class` in C++ sits in the tag name space. I wrote Bjarne in the 1980s asking him to remove it from the tag name space, as the tag name space is an abomination. He replied that there was too much water under that bridge. D doesn't have the tag name space, and in 20 years not a single pers…

The only explanation I saw was that C++ standards guys were horrified by the idea of unpredictable side effects as a result of initialization of a struct. I think C++ though is adding them. What I'd like in c is designated function parameters. // these the same bar(.a = 10, .b = 12); bar(.b = 12, .a = 10);

> The only explanation I saw was that C++ standards guys were horrified by the idea of unpredictable side effects as a result of initialization of a struct.

I don't understand. How would struct or class initialization be any different from simply doing, say, `for (auto& a : { x, y, z }) frob (a);` which is perfectly legal?

Re: Learning that you can use unions in C for grouping things into namespaces

#105
post #96

Earlier quoted context omitted.

... ? That's definitely not true, both anonymous structs and enums work fine in c++. https://wandbox.org/permlink/ICaQJXCaVOt9mXdP

No, they are forbidden by the standard (take a look at cppreference). Some compilers implement the C behavior as an extension, so tell your compiler to follow the standard strictly. I don’t use extensions, even convenient ones, as I have to be able to run my code on a variety of compilers. If you don’t have to do that, some extensions (like this one) are really handy.

From the standard, the enum-name is marked as optional in the enum grammar: in [dcl.enum](https://eel.is/c++draft/dcl.enum#11) ; it is also referenced e.g. in [dcl.dcl]:

> An unnamed enumeration that does not have a typedef name for linkage purposes ([dcl.typedef]) and that has a first enumerator is denoted, for linkage purposes ([basic.link]), by its underlying type and its first enumerator; such an enumeration is said to have an enumerator as a name for linkage purposes.

And for classes/structs, [class.pre](https://eel.is/c++draft/class.pre#def:class,unnamed) has explicit wording:

> A class-specifier whose class-head omits the class-head-name defines an unnamed class.

So both are entirely fine (and likewise, unions are too).

Note that my links are for the current draft, but I just checked and this was already the case as far back as C++11. So I wonder where this persistent myth seems to come from.

Re: Learning that you can use unions in C for grouping things into namespaces

#106

Earlier quoted context omitted.

I'm referring to both syntax based (AST) macros, and text based (preprocessor) macros. The latter, of course, are much worse. An example of the former is so-called "expression templates" in C++. I've seen them used to create a regular expression language using C++ expression templates. The author was quite proud of them, and indeed they were very clever. However nice the execution, the concept was terrible. There was…

> Anytime macros are used for metaprogramming, it's time to reach for a more powerful language. > I'm referring to both syntax based (AST) macros ... This surprises me greatly. Various lisps are among the most powerful languages I know of and a large part of the reason is macros coupled with their ability to execute arbitrary code at compile time (which itself uses additional macros, which in turn invoke more code, a…

[deleted]

Re: Learning that you can use unions in C for grouping things into namespaces

#107

Earlier quoted context omitted.

> is not a terrible thing It is a terrible thing. It's possible to do without them, and you'll like your code better. Your symbolic debugger and syntax directed editor will work properly. The poor schlub who has to fix the bugs in your code after you leave will be grateful. Your spouse will be happy and your children will prosper. For example, #define foo(x) ((x) + 1) replace with: int foo(int x) { return x + 1; } Th…

Having made a significant contribution to Simon Tatham's PuTTY, I have to respectfully disagree. At the time the entire SSHv2 key exchange and user authentication protocols were implemented in a single function using a massive Duff's device (for asynchrony) implemented with C metaprogramming macros. It was a surprisingly pleasant experience.

There is a reason why such clevernesses are forbiden by security standards like MISRA-C.

Re: Learning that you can use unions in C for grouping things into namespaces

#108

Earlier quoted context omitted.

I'm referring to both syntax based (AST) macros, and text based (preprocessor) macros. The latter, of course, are much worse. An example of the former is so-called "expression templates" in C++. I've seen them used to create a regular expression language using C++ expression templates. The author was quite proud of them, and indeed they were very clever. However nice the execution, the concept was terrible. There was…

> Anytime macros are used for metaprogramming, it's time to reach for a more powerful language. > I'm referring to both syntax based (AST) macros ... This surprises me greatly. Various lisps are among the most powerful languages I know of and a large part of the reason is macros coupled with their ability to execute arbitrary code at compile time (which itself uses additional macros, which in turn invoke more code, a…

I'm a noob when it comes to Lithp. But I'm told what happens with their macros is the language is fairly unusable until you write lots of macros. The macros then become your personal undocumented wacky language, which nobody else is able to use.

I've seen this happen with assembler macro languages, too.

> most powerful

It's like putting a 1000 hp motor in a car. It's main use is to wreck the car and kill the driver.

BTW, D is the first language of its type (curly brace static compilation) to be able to execute arbitrary code at compile time. It started as kind of "let's see what happens if I implement this", and it spawned an explosion of creativity. It has since been adopted by other languages.

Re: Learning that you can use unions in C for grouping things into namespaces

#109

Earlier quoted context omitted.

I'm referring to both syntax based (AST) macros, and text based (preprocessor) macros. The latter, of course, are much worse. An example of the former is so-called "expression templates" in C++. I've seen them used to create a regular expression language using C++ expression templates. The author was quite proud of them, and indeed they were very clever. However nice the execution, the concept was terrible. There was…

Can you link to it? We're using expression templates on a new library and I find it useful.

Oh man, you sure know how to drive a stake in my heart! What have I done! Some curses should just not be uttered. I'm not sure where it is on my backups.

But it was done just like you'd do it in C++. The same thing, just with D syntax.

Re: Learning that you can use unions in C for grouping things into namespaces

#110

Earlier quoted context omitted.

> clarity of intent Conditional compilation is an optimization, not a semantic intent. > non-optimized debug builds are affected The code size will be larger. Doesn't matter.

> Conditional compilation is an optimization, not a semantic intent. Why can't it have semantic intent? I frequently use it for cross-platform adjustments, and those don't usually compile on the defined-away platform.

> those don't usually compile on the defined-away platform

Then you're doing it wrong :-/

All these can be made to work.

P.S. compiling successfully is not the same thing as linking successfully. Think stubs and deciding which files to link together.

Post reply on HN