Why C++ is the ideal language for Web development

Revision en1, by ivan100sic, 2021-06-01 03:27:58

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

Tags #c++ 17

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English ivan100sic 2021-06-01 03:27:58 775 Initial revision (published)