Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Before delving into a project, I just want to make certain my understanding is correct as I haven't played with this before.

If I create a cross-platform program, the standard way to have the platform-specific code is to put it in a .h / .cpp file combo and #include it based on a header definition:

C++
#ifdef WINDOWS
   #include "WindowsSpecificXXX.h"
#elseif APPLE
   #include "AppleSpecificXXX.h"
   ...
   #endif

If I want to do this with constexpr, each .h / .cpp file combination must have all of the code in it for all the supported platforms. So each file becomes 2 to 5 times (or more) larger, depending upon how many platforms are supported:

C++
//In common header somewhere:
enum Platform { Apple, Windows };
constexpr Platform platform = Apple;

//And in specific implementation file:
void  createWindow() {
   if (platform == Apple) {
      //Apple stuff
      }
   else if (platform == Windows) {
      //Windows stuff
      }
   }

Am I missing something, like a way to include a file based on a constexpr, so the individual files don't get bloated? Are there other drawbacks to using constexpr in that manner besides large file sizes if my understanding is correct? Do modules offer a better solution to this problem?

Thanks!

What I have tried:

Some reading on constexpr, but not a lot.
Posted
Updated 21-Jan-22 5:36am
v2
Comments
Shao Voon Wong 20-Jan-22 22:38pm    
createWindow() will fail to compile under Windows Compiler because Apple code is visible to it. Likewise for Apple compiler.
David O'Neil 20-Jan-22 22:49pm    
Ahh. So the constexpr evaluation step is performed after the parsing and initial compilation step, whereas the preprocessor macros actually occur - as the name implies - in the preprocessing step? And the Apple code cannot pass through the compilation step. I'm pretty sure your answer is yes, but I will ask it to ensure my comprehension.
CPallini 21-Jan-22 2:31am    
Yes, the preprocesso has nothing to do with constexpr. Have a look at
https://blog.tartanllama.xyz/if-constexpr/
David O'Neil 21-Jan-22 2:59am    
Thanks!
Shao Voon Wong 21-Jan-22 2:52am    
If you make createWindow() a constexpr function and the if condition a constexpr if condition, the result is the same. All the code paths/branches in constexpr if must make sense to the C++ compiler.

https://en.cppreference.com/w/cpp/language/if

1 solution

I define an abstraction layer. Header files declare classes that a target operating system must implement, so they contain no platform-specific code. Each implementation file for such a header is platform specific and has a name like *.win.cpp. The entire contents of such a .cpp are included or excluded by an #ifdef OS_WIN...#endif, with OS_WIN being defined for the compile in question.

A #define for the platform type is the usual way to do this, along with #ifdef to include or exclude the code. If your header files are platform specific, you're also going to have platform-specific code throughout your implementation, which is a mess unless there are very few platform-specific things.
 
Share this answer
 
Comments
David O'Neil 20-Jan-22 21:09pm    
Have you ever played with constexpr for this purpose, as I outlined in the question? If not, what are your reasons for not doing so? From what I gather in this response, your answer is that the #ifdef approach for the entire .cpp file is more powerful of an approach than the alternatives, because the entire project can have all of the files for all of the platforms in it, and the #ifdefs encompassing the entire .cpp file (so the corresponding #endif is at the very bottom of the file) ensures that only the .cpp file for the corresponding project is really incorporated.
Greg Utas 21-Jan-22 7:57am    
I see that others have explained why constexpr can't be used for this purpose.
Greg Utas 21-Jan-22 8:25am    
It looks like you're trying to do GUI stuff on multiple platforms. I don't do GUI stuff, but I would try https://www.wxwidgets.org/ if I did. Open-source and free.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900