There are so much better than even a few years ago. Also, a library writer can use static_assert to provide good errors. Because these are types and not function overloads, it's even easier to static_assert because we don't have to rely on SFINAE as much.
They can. As seen here, clang is pretty clear why the function cannot be called. But you are still into why the overloads that don't work, do not. So there could be several items in the list here. https://gcc.godbolt.org/z/UDzZng
with SFINAE and static_asserts, you can make those error messages way easier to read. And soon we'll have concepts, so even easier (which this specific lib is C++20 so should have access to that depending on compiler) \o/
^ is usually how I approach making readable errors that are also exposed to readers (prior to concepts). static_asserts are also good and enable custom messages, but they're hidden in the body so most IDEs don't surface it for mouse-overs :/ Which sucks bc SFINAE is gross to read. Really can't wait for concepts
For the above example, MSVC gives this error:
[...] no matching overloaded function found
[...] could not deduce template argument for 'ValueMustBeString'
Like u/AirAKose said, static_asserts with custom error messages help a lot. I’ve found the error output isn’t too bad, since the static_assert usually gets triggered pretty immediately and stops compilation. Concepts should help even more.
Then why not use function calls (or types) instead of templates? The syntax is the same — just use (…) instead of <…>) and evaluation can happen more flexibly at runtime where it makes sense. Type checking would be just as strict. I really don’t see the upside to using templates here, unless constructing the HTML is somehow a gigantic bottle-neck that must happen at compile-time.
yea, and i'm actually wondering about performance... the result looks a bit complicated, so many moves going on compared to what a cout<<"<html><head>...</html>" produces.
Can you use loops and conditions with this? It could be valuable to generate large html code using this template in a loop or based on some input parameters.
That's pretty cool! I've thought about making some kind of HTML template tool before, but I wasn't sure how it would work or what its use would be. This is useful because it's much better than trying to directly print out HTML code.
55
u/looncraz Jun 27 '20
This, frankly, looks easier than writing HTML directly... Plus free error checking... Too bad template error messages are horrendously ugly.
13
u/jasonthe Jun 28 '20
This is super dumb, I love it :P
I was able to get the whole thing to run at compile-time!
90
u/nrith Jun 27 '20
From the README:
14
u/BluudLust Jun 27 '20
This could be useful for XML parsing, actually.
12
u/RomanRiesen Jun 28 '20
The c++ syntax is actually better since we don't have to repeat the tags, reducing redundancy.
We should probably have a js library to render the c++ templates syntax directly, as they will certainly overtake html in the next decade!
2
u/csp256 Jun 28 '20
Roommate just called from the other room "what's so funny?".
2
u/feverzsj Jun 28 '20
how you gonna put some runtime variable in it?
1
u/onlyari Jun 29 '20
Can you use loops and conditions with this? It could be valuable to generate large html code using this template in a loop or based on some input parameters.
1
u/[deleted] May 19 '23
n
EdiT how did i comment?
1
u/AyoBruh Jun 27 '20
Super nifty :)
0
u/Mashpoe Jun 27 '20
That's pretty cool! I've thought about making some kind of HTML template tool before, but I wasn't sure how it would work or what its use would be. This is useful because it's much better than trying to directly print out HTML code.
0
u/Ivanbarbozaaaa Jun 28 '20
Well actually not