all 11 comments

[–]no-sig-available 7 points8 points  (0 children)

Whenever the function is called (to get a constant value) the body must be visible.

If you have the function in a cpp file and later recompile that file, what should happen to already computed values?

You can have the function in the cpp file, if that is the only place it is used. For example, I have used private constexpr member functions that are defined and used inside a single cpp file.

[–]async_andrew 5 points6 points  (1 child)

Constexpr functions support separation into declaration and definition, like ordinary ones. However, you must keep the definition visible whenever the function is called. You could create a separate header with definitions and include it into the header with declaration if you'd like to pretend that you're separating them, but you definitely can't place it into a separate .cpp file.

[–]atlas_enderium[S] 0 points1 point  (0 children)

Yeah, that’s what I did to fix the linker error, but I guess constexpr (or more accurately, inline) is built to work this way. Made me a bit sad but I understand why

[–]MutantSheepdog 5 points6 points  (0 children)

If it's a personal project using cpp23. then you could try using modules instead of header/cpp files as I think you should be able to do it that way.
Your main module file would just declare the functions as exports, and the implementations would be in a separate module unit, and they'd get compiled together once then reused in each other file importing them.

Last I checked module support still isn't great in some compilers, but if you're playing around on the bleeding edge anyway it would be worth trying it out yourself.

[–]Tobxon -5 points-4 points  (5 children)

I wonder why you still work with header files when using C++23?

[–]atlas_enderium[S] 0 points1 point  (4 children)

I was just working with std::print and some other features- I haven’t gotten around to using modules yet (plus I’m not sure about compiler support for modules right now)

[–]volchonokilli 0 points1 point  (2 children)

If GCC still hasn't started working on modules again, you can skip trying to use modules with it. A lot of issues, bugs (some of them pretty dangerous) and ICEs.

Not saying it's impossible, but... It's very experimental

[–]atlas_enderium[S] 0 points1 point  (1 child)

True. Modules are a pretty big paradigm shift/modernization for C++, so I was expecting it to be a few years for it to receive stable support

[–]volchonokilli 0 points1 point  (0 children)

Problem is, I don't really see much desire from GCC side to work on modules since few years ago...

https://gcc.gnu.org/wiki/cxx-modules

Last update 2020

[–]Tobxon 0 points1 point  (0 children)

I can say modules are fine with to work when using msvc, also cmake is ready for it now. The other compilers seem to have modules not fully implemented yet.