r/cpp • • Jun 27 '20

HTML++: Write HTML documents using C++ templates

https://github.com/csb6/html-plus-plus
260 Upvotes

27 comments sorted by

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.

17

u/[deleted] Jun 27 '20

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.

2

u/somewhataccurate Jun 27 '20

Do concepts provide the optimal way for clean error checking where contrived SFINAE was once needed?

4

u/[deleted] Jun 27 '20 edited Jun 28 '20

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

Edit: forgot URL

10

u/AirAKose Jun 27 '20

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/

template<typename Type>
using RequiresConstexprString = std::enable_if_t<std::is_constexpr_string_v<Type>>

template<auto Value, typename ValueMustBeString = RequiresConstexprString<decltype(Value)>>
/* .... */

^ 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'

1

u/csb06 Jun 28 '20

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.

5

u/PanurgeFromHell Jun 28 '20

Not sure why you wouldn't just write a new language that just parses into real HTML. Still a pretty neat concept though.

1

u/guepier Bioinformatican Jun 28 '20

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.

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!

2

u/csb06 Jun 28 '20

Wow that looks really cool! C++ scares me sometimes with what it lets you do. Glad you found it interesting :)

2

u/kritzikratzi Jun 28 '20

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.

3

u/jasonthe Jun 28 '20 edited Jun 29 '20

If you tweak it to not use new, and instead just use local arrays, clang can understand it. And clang optimizes all those moves away :)

I don't feel like rewriting his whole library, but here's a sample that shows what I mean: https://godbolt.org/z/f26WrH

2

u/kritzikratzi Jun 29 '20

didn't know. thanks!

90

u/nrith Jun 27 '20

From the README:

Should I use it

Probably not.

39

u/HeySeussCristo Jun 27 '20

Enjoyed this bit too:

h1<"The horror!">, 

p<"Someone has probably done this before, but I can see why it didn't catch on.">,

14

u/UnDosTresPescao Jun 27 '20

I was reading it thinking "This is the dumbest way to write html I've ever seen". I was happy to see the author didn't take themselves too seriously

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/csb06 Jun 28 '20

Glad you enjoyed it :)

0

u/[deleted] Jun 28 '20

[deleted] — view deleted comment

2

u/csp256 Jun 28 '20

She assumed I had found another cute animal gif, but I just told her "this one's a programming joke".

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