r/cpp • u/dwrodri • Apr 26 '26
"Parse, don't Validate" through the years with C++
https://derekrodriguez.dev/parse-dont-validate-through-the-years-with-c-/It's been a few years since I worked with C++ on a day-to-day basis in a professional capacity, but I like writing blog posts every now and then, and thought I would put my own spin on the evergreen "Parse, don't validate", by Alexis King. Feedback is welcome!
55
Upvotes
12
u/Infinite_Reference17 Apr 26 '26
The post says it compiled with $(CXX) --std=$(CXXVERSION) file.cpp
Does this mean you benchmarked unoptimized code? (There is an -o2 or -o3 missing)
12
u/yeolhan_ian Apr 27 '26
I had never read "Parse, don't validate"; both articles were cool (and entertaining) to read. I'd just like to point out a potential refinement for the C++23 version; for that implementation you use
std::expectedas the return type forBirthdate::parse_n_digits, but the function can only ever err in one way, and that way is provided at the call site as well. I think it would make more sense to return astd::optionalhere instead of giving the function the error type to propagate back up; the function doesn't need to know about it at all. I don't write much C++ so I could be missing something, but if this were rust (which I'm more familiar with) I'd haveparse_n_digitsreturnOption<i32>and then do something like this (sorry for the rust jumpscare in the cpp subreddit lol):so maybe in c++ the call site would look something like this:
No hate intended though, I really enjoyed the article!
3
u/dwrodri Apr 27 '26
The control flow for checking correctness is never gone entirely! It’s all about where you put it. Throughout my examples, I hop from
sscanfto manual parsing tostd::get_timeand back.But the more you put up front (i.e. in the type construction), the less pain you will experience when writing code which depends on your parser.
27
u/vicentezo04 Apr 27 '26
I had to re-read this and the linked articles a few times. "Wait, it just looks like the parsing part is just doing a very thorough validation".
But then it clicked...
- Parsing = trying to form a complete fully-hydrated object from the given input
- Validating = passing the object through a hand-wavy function that says "looks good to me" and risking downstream callers saying "wait, we're missing X"
As a person who writes .NET for a living, it's nice to see C++'s newer features like `std::exepected`. It's definitely better than having a dozen different types of error structs/classes/tuples patterns in the codebase depending on who worked on the code.
4
u/pavel_v Apr 27 '26 edited Apr 27 '26
Out of curiosity decided to create my C++23 version of your code and went down the rabbit hole because it was way slower to build than the one that you wrote. Turned out that using the
std::formatfor formatting/printing the user defined types was the "biggest offender" of the build times.Below are the results on my machine (AMD Ryzen 5 7535HS, 16 GB DDR5 4800, gcc 14.2):
parse_date_org.cpp- the original version of the codeparse_date_ios.cpp- my version usingiostreamfor formatting the user defined typesparse_date_fmt.cpp- my version usingfmtlibfor formatting the user defined typesparse_date_std.cpp- my version usingstd::formatfor formatting the user defined typesHere are all of the tested versions of the code.
All of my versions are exactly the same and only the formatting/printing of the user defined types is different.
2
u/developer-mike Apr 27 '26
Great posts, yours and the one you linked, which I hadn't seen!
I'm curious how you draw the line vs correctness by construction.
For instance, I kind of like inversion of control involving
clamp, and instead doingtemplate<typename T, int max, int min> class clamped;, and making your date class accept aclamped<int32_t, 1, 12> month, clamped<int32_t, 1, 31> day, etc.There's also risk that some of the offsets you're using aren't correct. It's hard to hear "parse, don't validate" and not imagine something that works linearly consumes input from a buffer. Something that manages the pointer into the date string, so you can more fluently call
take4(),take('-'),take2(), ...I guess my question is really, would you say these ideas are too far to be considered parse don't validate?
1
u/jepessen Apr 28 '26
I don't know...the idea can be good but there's a lot of coupling. What if I need to use another file format? Or if I need to parse binary data instead than a string? I see a lot of coupling here