[Development] better compile-time os detection

EXT Tim Blechmann tim.blechmann at qt.io
Fri Jul 24 08:07:58 CEST 2026


>> depending on the code, one could utilize tag dispatching, so only then
>> overload declarations would require the preprocessor.
>> ```
>> #ifdef LINUX
>> auto rename(auto src, auto target, Os::LinuxT) {
>>       return renameat2(...);
>> }
>> #elif DARWIN
>> auto rename(auto src, auto target, Os::DarwinT) {
>>       return renameatx_np(...);
>> }
>> #endif
>>
>> auto rename(auto src, auto target)
>> {
>>       return rename(src, target, Os::Current); // no ifdef
>> }
>> ```
> 
> Why would you do that? What's the advantage? If you remove the tag, it would
> compile to the same thing.
codegen should be identical. some benefits:
* no ifdef at the caller site
* better support for hierarchies (i.e. Os::Current could be "macos", but 
would match "darwin"). one can easily introduce an overload for "macos" 
later
* tag dispatching composes quite nicely

it's not a common use case, especially in qt. but every once in a while 
it can come in handy


More information about the Development mailing list