Live data from Hacker News

C++: Zero-cost static initialization

cofault.com

31–40 of 60 posts

Re: C++: Zero-cost static initialization

#31

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…

Why not just use constexpr in the second case?

What if the object is to be mutable?

Re: C++: Zero-cost static initialization

#32

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…

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?

Re: C++: Zero-cost static initialization

#33
post #16

> For this we need a certain old, but little-known feature of UNIX linkers STOP WRITING NON-PORTABLE CODE YOU BASTARDS. The correct answer is, as always, “stop using mutable global variables you bastard”. Signed: someone who is endlessly annoyed with people who incorrectly think Unix is the only platform their code will run on. Write standard C/C++ that doesn’t rely on obscure tricks. Your co-workers will hate you le…

Every time someone ships successful code that's hard to port to Windows the world becomes a better place.

Attitudes like this are the absolute worst. Fellow HN readers, don’t be like this person.

Re: C++: Zero-cost static initialization

#34

Earlier quoted context omitted.

> Every time someone ships successful code that's hard to port to Windows Until your boss tells you to port your so-far Linux-only code to Windows, and you run that struggle. Signed, someone who spent the past year or so porting Linux code to Windows and macOS because the business direction changed and the company saw what was the money-maker. P.S. Not the parent commenter, because I just realised they, too, had a pa…

Can you setup Windows to install WSL if it isn't there yet and then set it up, in a Windows installer?

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.

Re: C++: Zero-cost static initialization

#35
post #16

Earlier quoted context omitted.

Every time someone ships successful code that's hard to port to Windows the world becomes a better place.

Attitudes like this are the absolute worst. Fellow HN readers, don’t be like this person.

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

Re: C++: Zero-cost static initialization

#36

Earlier quoted context omitted.

Attitudes like this are the absolute worst. Fellow HN readers, don’t be like this person.

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? Virtual reality. You know what platform doesn’t have a way to do VR currently? Linux. Womp womp sad trombone. Mistakes were made.

It’s really easy and not hard to write crossplatform code. My experience is that Linux devs are by far the most resistant to this. It’s very annoying.

Should you care about Windows? I certainly think so. Linux still doesn’t have a good debugger (no, gdb/lldb) aren’t good. Quite frankly every Linux dev would be more productive if they supported Windows where debuggers exist and are decent. So really they’re just shooting themselves in the foot. IMHO.

Re: C++: Zero-cost static initialization

#37

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…

There are probably international traders who can tell a similar story with Linux replaced with "the US" and Windows replaced with "Russia", but I wouldn't consider that similar story to be an argument for ending the sanctions on Russia.

However, there is an important difference between the sanctions on Russia and the strategy of the pro-Linux "activist" that started this thread: namely, Windows is so heavily entrenched in the niche of enterprise IT that there is no significant chance of Linux's replacing it in that niche with the result that there is no realistic chance of a positive effect of this activism that might cancel out the negative effect you describe. So, I am tentatively in agreement with you.

Re: C++: Zero-cost static initialization

#38

Earlier quoted context omitted.

Why not just use constexpr in the second case?

What if the object is to be mutable?

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

Re: C++: Zero-cost static initialization

#39

Earlier quoted context omitted.

Can you setup Windows to install WSL if it isn't there yet and then set it up, in a Windows installer?

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/Windows implementation instead, do you like that better?

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

Re: C++: Zero-cost static initialization

#40
post #38

Earlier quoted context omitted.

What if the object is to be mutable?

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?
Post reply on HN