Take a look at this snippet:
#include <iostream>
#include <string>
using namespace std;
template<const char* tag>
struct html_element {
string s;
template<typename... Args>
html_element(Args... args) : s((... + (string)args)) {}
operator string() { return string("<") + tag + ">" + s + "</" + tag + ">"; }
};
const char html_c[] = "html";
const char body_c[] = "body";
const char h2_c[] = "h2";
using html = html_element<html_c>;
using body = html_element<body_c>;
using h2 = html_element<h2_c>;
int main() {
string doc{
html{
body{
h2{ "hello" },
h2{ "world" }
}
}
};
cout << doc << '\n';
}
Thank you for your attention
That definitely looks like Reactjs or something similar to me
May I direct your attention to https://github.com/csb6/html-plus-plus
What will happen in this code if you want to use "img" in html?
is this some kind of peasant joke im not able to understand
There is also the crow framework for proper web development in C++. LinkedIn has a good course for it.
Unpopular opinion: C++ is both the best and the worst language, for any purpose.
Okay, this does like awesome at first but then you find out you miss a couple of things that make this code terribly inefficient. For example:
This invokes copy constructor of
args
which may be inefficient. Or maybe not, if copy elision takes place. Either way you probably don't want to hope it works as you expect it to, so you should probably add a universal reference. Also this is going to break ifargs
is empty, and I wouldn't recommend C-style cast, and you should perfect-forward arguments, something like this:Then, you most likely want to construct HTML in-place and not copy it here and there, so you may think it's a good idea to make
std::string
takethis
via an rvalue reference:This code may look fine but it still involves copying because a big string is still constructed every time via concatenation. You may think a
std::string_stream
would work well:But you'd still have
std::string s
member which would ruin performance. Honestly I have no idea how to fix this except resorting to slow-compilation compile-time template-based strings. Or just using a template compiler instead, which you should be using anyway.Disclaimer: I haven't tried to compile any of these codes.