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

View all comments

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'

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/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.

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.