Live data from Hacker News

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

utcc.utoronto.ca

1–10 of 150 posts

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

#3
post #2

Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” member).

In C++ we have namespaces for 30 years now, no need for such tricks.

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

#4
post #3
post #2

Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” member).

In C++ we have namespaces for 30 years now, no need for such tricks.

C++ namespaces are unrelated to this. They don’t accomplish the same thing.

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

#5
post #3
post #2

Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” member).

In C++ we have namespaces for 30 years now, no need for such tricks.

Hmm. How do C++ namespaces help with the structure naming problem in this example? They seem completely orthogonal.

C++ namespaces are a way to avoid library A's symbol "cow" clashing with library B's symbol "cow" without everything being named library_a_cow and library_b_cow all over the place which is annoying. I agree C would be nicer with such a namespace feature.

However this technique is about what happens when you realise your structure members x and y should be inside a sub-structure position, and you want both:

d = calculate_distance(s.x, s.y); // Old code

and

d = calculate_distance(s.position.x, s.position.y); // New

... to work while you transition to this naming.

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

#6
post #2

Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” member).

In other words: please always be wary of differences in C and C++, for instance type punning [0].

[0] https://stackoverflow.com/a/25672839

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

#8
post #3

Earlier quoted context omitted.

In C++ we have namespaces for 30 years now, no need for such tricks.

Hmm. How do C++ namespaces help with the structure naming problem in this example? They seem completely orthogonal. C++ namespaces are a way to avoid library A's symbol "cow" clashing with library B's symbol "cow" without everything being named library_a_cow and library_b_cow all over the place which is annoying. I agree C would be nicer with such a namespace feature. However this technique is about what happens when…

You can use inline namespaces for versioning symbols.

https://www.foonathan.net/2018/11/inline-namespaces/

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

#9
post #4
post #3

Earlier quoted context omitted.

In C++ we have namespaces for 30 years now, no need for such tricks.

C++ namespaces are unrelated to this. They don’t accomplish the same thing.

The goal of inline namespaces is exactly to allow for migrating libraries across versions.

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

#10
I'm using anonymous nested structs extensively for grouping related items, but I consider the extra field name a feature, not something that should be hidden:

https://github.com/floooh/sokol-samples/blob/bfb30ea00b5948f...

(also note the 'inplace initialization' which follows the state struct definition using C99's designated initialization)

Post reply on HN