←back to thread

48 points zigrazor | 8 comments | | HN request time: 0s | source | bottom

Hi HN!

I've built [CXXStateTree](https://github.com/ZigRazor/CXXStateTree), a modern C++ header-only library to create hierarchical state machines with clean, intuitive APIs.

It supports: - Deeply nested states - Entry/exit handlers - State transitions with guards and actions - Asynchronous transitions with `co_await` (C++20 coroutines) - Optional runtime type identification for flexibility

It's ideal for complex control logic, embedded systems, games, robotics, and anywhere you'd use a finite state machine.

I’d love feedback, use cases, or contributions from the community!

Repo: https://github.com/ZigRazor/CXXStateTree

Show context
dgan ◴[] No.44523973[source]
i am by no means a C++ expert, but isn't "pragma once" frowned upon?
replies(5): >>44524039 #>>44526928 #>>44526935 #>>44528279 #>>44529097 #
kookamamie ◴[] No.44524039[source]
No, it is the way. Edit: no one has time for inventing unique names for include guards.
replies(2): >>44524438 #>>44524781 #
1. motorest ◴[] No.44524781[source]
> No, it is the way.

No, this is completely wrong. Pragma once is non-standard compiler directive. It might be supported by some compilers such as msvc but technically it is not even C++.

There are only two options: include guards, and modules.

replies(2): >>44524794 #>>44524905 #
2. quietbritishjim ◴[] No.44524794[source]
What major compiler does not support it?
replies(1): >>44524934 #
3. spacechild1 ◴[] No.44524905[source]
Yes, it is non-standard, but I don't know any compiler that does not support it.
4. motorest ◴[] No.44524934[source]
> What major compiler does not support it?

The whole point is that it's not supported and it's not standard, thus using #pragma once needlessly introduced the risk of having the code break.

You should ask yourself what are you doing and why are you using non-standard constructs that may or may not work, specially when it's rather obvious and trivial to just use include guards. Using #pragma once isn't even qualify as being clever to gain anything.

replies(3): >>44525100 #>>44525869 #>>44527946 #
5. phkahler ◴[] No.44525100{3}[source]
Maybe you should ask why you're using a non-standard compiler if it's not supported.

Not being part of the official standard doesn't necessarily mean it's not well supported.

6. AndriyKunitsyn ◴[] No.44525869{3}[source]
The whole point of C/C++ is knowing your environment. It's not Java, it's not TypeScript. It's one level above assembly, and if you change the compiler and things break, then it's your fault.

If the standards still don't have a proper replacement for include guards, then too bad for the standards. The C++ standard wasn't aware of multithreading before C++11, this didn't stop people from writing multithreaded programs.

As to why - #pragma once is just cleaner to look at.

replies(1): >>44527061 #
7. gpderetta ◴[] No.44527061{4}[source]
> the standards still don't have a proper replacement for include guard

It does with modules... and in ten year if modules support is widespread, I'll consider stoping using pragma once.

8. AlotOfReading ◴[] No.44527946{3}[source]
Lots of things in C/C++ are nonstandard or optional, like the existence of optimization flags. Nevertheless, it's supported by literally every compiler I can think of for at least the last decade. I had to get into weird interpreters written in Python for university projects before I found anything that didn't support it.

Plus, it's nicer to read than #ifndef FOO_BAR_BAZ_PROJ_DIR1_DIR2_DIR3_FILE_H

#endif /* FOO_BAR_BAZ_PROJ_DIR1_DIR2_DIR3_FILE_H */

On every file.