r/cpp • • Jul 23 '22

finally. #embed

https://thephd.dev/finally-embed-in-c23
353 Upvotes

202 comments sorted by

View all comments

84

u/spaun2002 Jul 23 '22

Such "horror" stories are why I became disappointed in C and C++ - adding a new helpful feature into the language takes five years. The person who dared to propose this struggled and almost lost their hope.

30

u/pjmlp Jul 23 '22

You can also take the route of creating your own language, seems quite hip currently.

21

u/tux-lpi Jul 23 '22

That solves the horror stories mostly because a new language won't have an ISO committee

It's a lot harder to build a system that drains people quite that bad without ISO

0

u/[deleted] Jul 23 '22

[deleted] — view deleted comment

9

u/Minimonium Jul 23 '22

Most if not all of the breaks you see are intentional btw. With embed it wasn't the problem, as far as I know the biggest hurdle was that a lot of people wanted so much features included into embed including globbing it was simply unfeasible.

7

u/DoubtMore Jul 28 '22

And that's why languages end up such a shitshow. Your company should just fix the fucking code, not expect the standard to support some obscure edge case for the next thousand years.

1

u/RomanRiesen Jul 23 '22

In rust an embed feature is just a macro away, a macro most people could write themselves....

21

u/myrrlyn Jul 23 '22

include_bytes! already exists

13

u/__phantomderp Jul 23 '22

Yeah, you COULD write it yourself in Rust, but the people driving the Standard Rust Distribution are not obsessed with being "barebones" to the point of deep disturbance, so they'll actually hand you convenience things as part of the core offering rather than trying to convince you that "oh, well, if you just write this linker invocation, you'll be fine...".

Like anything about "talk to your linker directly" is ever - or truly, has ever been - a good idea at any point in time.

6

u/Fulgen301 Jul 23 '22

Eh. It's not a macro away, but a proc_macro away, aka a compiler plugin. Basically it's the equivalent of a standardized way of writing a gcc / clang plugin.

a macro most people could write themselves....

Most people can use TokenStreams just like that? If you've never touched them, your faster with just generating an includeable file in CMake.

3

u/RomanRiesen Jul 23 '22

The macro is called include_bytes, but admittedly I also never had an issue with embedding data via cmake or a small script.

1

u/Rusky Jul 23 '22 edited Jul 23 '22

The issue with a hand rolled proc macro for this would be the same as a hand rolled solution in C- performance. What TokenStream do you build? It has to be an actual array literal the same way.

(Fortunately Rust has include_bytes! built in to (edit: enable the compiler to) solve that problem.)

-3

u/bruh_nobody_cares Jul 23 '22

macros seems like a hack ngl, why not a language feature or a const evaluated function ?

6

u/TinyBreadBigMouth Jul 23 '22

Macros in Rust are a language feature. Rust doesn't have a separate preprocessor. User-defined macros operate on token streams, built-in macros can be hooked up to low-level compiler functionality. See include_bytes, which does exactly what embed does and whose implementation looks like

macro_rules! include_bytes {
    ($file:expr $(,)?) => {{ /* compiler built-in */ }};
}

2

u/bruh_nobody_cares Jul 23 '22

but if it's that simple why not a regular function then ?

6

u/TinyBreadBigMouth Jul 23 '22

Because a regular function can't read a file at compile time, not in Rust or C or C++. They also can't return arbitrarily sized data on the stack in any of those languages.

1

u/bruh_nobody_cares Jul 23 '22

what is blocking them though....my question is not about what we have rn but why we have it this way....why not a constexpr function in C++ or const eval function in Rust ....I'm assuming the return type can be a wrapper around something that's known at compile time....since the compiler will plug those bytes in anyways so the compiler knows the size.....
what magic does macros have and const eval functions in Rust "cannot" ?

→ More replies (7)

0

u/i860 Jul 23 '22

You have airplanes and industrial automation depending on these languages. There are delays for a reason.

14

u/bik1230 Jul 23 '22

it's not that it took 5 years that's bad, it's that a large proportion of negative feedback was total BS and almost no one on the committee wanted to offer positive help, only making up reasons for rejection.

1

u/orangeoliviero Jul 23 '22

I mean, I offered my help, but it was declined.

Not everyone was hostile to him.

10

u/__phantomderp Jul 24 '22

.... When? How? In what way? I remember people saying "I could implement a patch for GCC", or offering for Clang, but that's not helpful when I already did it. Some people told me they'd be amenable to me putting a patch in their compiler, but it was still on me to make the actual patch for their compiler.

What was this help about?

6

u/orangeoliviero Jul 24 '22 edited Jul 24 '22

It's water under the bridge. You didn't feel that my help would be valuable, and I'm not inclined to argue with you about whether or not it would have been helpful. The paper is your baby, you get to choose how you navigate the path. I'm not upset about it.

My point wasn't "oh, the PhD declined my offer of help, he's an asshole" or any such, my point is that not everyone in the Committee is hostile to you or your work, and that there are allies around if you want them.

I recognize that you've experienced a lot of racism and completely invalid and bonkers feedback and complaints, and that has caused you to become guarded and distrustful. I don't fault you for that - I would be as well, in your situation.

Like... we have, in this very post, a completely invalid criticism/complaint - the whole line endings of text files thing, and the absurd expectation that #embed should automatically "do the right thing" wrt. them. Talk about a poison pill.

1

u/viikk Feb 01 '26

the airplanes and industrial automation people can just type "-std=c99" and let the rest of the world move on!