Live data from Hacker News

C++: Zero-cost static initialization

cofault.com

41–50 of 60 posts

Re: C++: Zero-cost static initialization

#41
post #11

Earlier quoted context omitted.

I tell my coworkers, "Hey, we need this coded up as a Windows service!" and I get crickets. So I spin up a Debian VM and POSIX the hell out of it. If they dare to complain, I tell 'em to do their damn jobs and not leave all the hard stuff to the guy that only programs on UNIX.

Their tasks would be less hard if the UNIX guy would stop writing non-portable POSIX code =P

That's what I know. I wasn't hired to program Windows machines, I was hired to program PLCs and SCADA systems. But every now and then something best done in .NET comes up and none of the Windows guys can get off their butts to do it. So then they get to whine about having to deal with Linux and I get to tell them to suck it up.

Sure, I could learn to program on Windows... Or I could pick up another PLC or SCADA platform that looks good on my resume. Guess which I choose to do?

Re: C++: Zero-cost static initialization

#42

Earlier quoted context omitted.

In theory I agree, but in practice this just increases compile time by a lot, and we need to be able to manually toggle the code that gets executed at compile time.

What? Edit: what?

When every eligible piece of code becomes automatically constexpr, as you suggest, the compile time will just balloon. The code still needs to be compiled and executed, just all at once now. Optimization is one of those annoying problems for which we currently don't have the compute to fully bruteforce it. We need to be selective with which code is marked constexpr.

Re: C++: Zero-cost static initialization

#43

Earlier quoted context omitted.

What? Edit: what?

When every eligible piece of code becomes automatically constexpr, as you suggest, the compile time will just balloon. The code still needs to be compiled and executed, just all at once now. Optimization is one of those annoying problems for which we currently don't have the compute to fully bruteforce it. We need to be selective with which code is marked constexpr.

> constexpr, as you suggest

I made no mention of constexpr.

Program start-up isn't compile time.

The idea is that since "static obj bar()" doesn't depend on anything in the function, it could in principle be moved outside of the function. So in actual fact, it can be treated that way by the loading semantics of the program (can be constructed without the function having to be called), except that the name bar is only visible inside the function.

I don't understand why C++ wouldn't have specified it this way going back to 1998, but that's just me.

Re: C++: Zero-cost static initialization

#44

Earlier quoted context omitted.

Forcing Windows users to use WSL is generally a non-starter. That’s just not how things work. You can force someone to change their entire execution environment without further consequences. If you only support WSL then you don’t support Windows, imho.

I see were you are coming from, but programs also depend on an SQL server, a python installation or a Java instance. You also don't complain about device drivers, support for filesystems, the network stack and hooks to Windows Explorer. In the end it is just part of the OS and a bunch of extra userspace programs. I mean nobody complains about the Windows Subsystem for Win32. But yeah, you can just use a non-MS GNU/Wi…

> Is it possible though? Is it possible to have isolated WSLs (per programm)?

Maybe. But my experience is that there is very little “program code” and it’s mostly “library code”.

And if you did have a program that required WSL and you followed the UNIX model of bash chaining programs then you’re now mandating the “meta program” be run under WSL.

I treat WSL as a hacky workaround for dealing with bad citizens. It’s not a desirable target environment. It exists as a gap stop until someone does it right. YMMV.

Re: C++: Zero-cost static initialization

#45
post #41

Earlier quoted context omitted.

Their tasks would be less hard if the UNIX guy would stop writing non-portable POSIX code =P

That's what I know. I wasn't hired to program Windows machines, I was hired to program PLCs and SCADA systems. But every now and then something best done in .NET comes up and none of the Windows guys can get off their butts to do it. So then they get to whine about having to deal with Linux and I get to tell them to suck it up. Sure, I could learn to program on Windows... Or I could pick up another PLC or SCADA platf…

I have nothing to nice to say in response. Good luck!

Re: C++: Zero-cost static initialization

#46
post #38

Earlier quoted context omitted.

You use constinit. But this means constexpr constructor (easy with 2-phase init, not too much of a problem for singleton objects), and trivial destruction

Does that guarantee that the static object is constructed before the first execution of the block?

Yes, it will be constructed at compile time. Not everything can be constinit.

Re: C++: Zero-cost static initialization

#47

Earlier quoted context omitted.

I see were you are coming from, but programs also depend on an SQL server, a python installation or a Java instance. You also don't complain about device drivers, support for filesystems, the network stack and hooks to Windows Explorer. In the end it is just part of the OS and a bunch of extra userspace programs. I mean nobody complains about the Windows Subsystem for Win32. But yeah, you can just use a non-MS GNU/Wi…

> Is it possible though? Is it possible to have isolated WSLs (per programm)? Maybe. But my experience is that there is very little “program code” and it’s mostly “library code”. And if you did have a program that required WSL and you followed the UNIX model of bash chaining programs then you’re now mandating the “meta program” be run under WSL. I treat WSL as a hacky workaround for dealing with bad citizens. It’s no…

> But my experience is that there is very little “program code” and it’s mostly “library code”.

Sorry I am confused, what does that refer to?

> you’re now mandating the “meta program” be run under WSL

As long as bash and the tools are in path, can't you run any program normally?

> WSL as a hacky workaround for dealing with bad citizens

Yes, but some parts are out of scope for C and you need to target the OS. Also f.e. passing around file descriptors and sockets are convenient.

Re: C++: Zero-cost static initialization

#48

The way block scope statics are handled in C++ is a mistake. Block scope statics that don't depend on any non-static local variables should be initialized when the program starts up. E.g.: void fun(int arg) { static obj foo(arg); // delayed until function called (dependency on arg) static obj bar(); // inited at program start (no dependency on arg) } In other words, any static that can be inited at program startup sh…

I think that would cause some surprising behavior changes for certain code changes, not something that C++ (or any language) needs more of.

Re: C++: Zero-cost static initialization

#49

The way block scope statics are handled in C++ is a mistake. Block scope statics that don't depend on any non-static local variables should be initialized when the program starts up. E.g.: void fun(int arg) { static obj foo(arg); // delayed until function called (dependency on arg) static obj bar(); // inited at program start (no dependency on arg) } In other words, any static that can be inited at program startup sh…

I think that would cause some surprising behavior changes for certain code changes, not something that C++ (or any language) needs more of.

Which is why it should have been sorted out decades ago.

Re: C++: Zero-cost static initialization

#50

Earlier quoted context omitted.

I'm not sure what to think. What argument do you have for your position?

Windows and *nix are different platforms. Writing crossplatform code is pretty easy and a solved problem. At least 99.5% of code can be platform agnostic. Only a few tiny bits are special. My background is video games. Which is well known to be a Windows-first environment. I currently work on robotics. The vast majority of robotics ecosystem code is Unix only. You know what is extremely useful for teleoperation? Virt…

You're complicit with Windows living on, being long overdue in the landfill of computing history.
Post reply on HN