{"id":2151,"date":"2024-10-30T20:50:20","date_gmt":"2024-10-30T20:50:20","guid":{"rendered":"https:\/\/cppdepend.com\/blog\/?p=2151"},"modified":"2024-10-30T20:57:20","modified_gmt":"2024-10-30T20:57:20","slug":"cpp2-for-c-serves-a-similar-purpose-as-typescript-does-for-javascript","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/cpp2-for-c-serves-a-similar-purpose-as-typescript-does-for-javascript\/","title":{"rendered":"Cpp2 for C++ serves a similar purpose as TypeScript does for JavaScript"},"content":{"rendered":"\n<p>TypeScript was developed to address several key challenges faced by developers working with JavaScript, particularly in large-scale applications. Indeed, <strong>TypeScript<\/strong> is a superset of JavaScript that introduces optional static typing and other features to enhance the development experience, particularly for large-scale applications. Its history reflects the growing need for better tooling and structure in JavaScript development.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Browsers do not recognize TypeScript directly; therefore, it must be transpiled into valid JavaScript code so that it can be executed by the browsers.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"489\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/10\/image-1024x489.png\" alt=\"\" class=\"wp-image-2152\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/10\/image-1024x489.png 1024w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/10\/image-300x143.png 300w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/10\/image-768x367.png 768w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/10\/image-1200x573.png 1200w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/10\/image.png 1400w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>CppFront, created by Herb Sutter, is an experimental tool designed to explore <strong>Cpp2<\/strong>, an alternative syntax for C++ that aims to improve the language&#8217;s usability, readability, and safety. Much like how <strong>TypeScript<\/strong> enhances <strong>JavaScript<\/strong> by adding type safety, modern syntax, and error detection, <strong>CppFront<\/strong> seeks to bring  improvements to C++ while retaining compatibility with existing code and libraries. <\/p>\n\n\n\n<p>And like TypeScript <strong>Cpp2<\/strong> is designed to work by first converting <strong>Cpp2<\/strong> code into standard <strong>C++ code<\/strong> before compiling. The tool that makes this possible is called <strong>CppFront<\/strong> which is similar to the TS compiler.<\/p>\n\n\n\n<p>Herb Sutter presented Cpp2 two years ago, and still actively working on it: <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Can C++ be 10x Simpler &amp; Safer?  - Herb Sutter - CppCon 2022\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/ELeZAKCN4tY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>CppFront  is introduced to help developers explore a safer and more expressive way of writing C++ code. Here&#8217;s how the process works:<\/p>\n\n\n\n<p>1- <strong>Cpp2 Syntax Parsing<\/strong>:<\/p>\n\n\n\n<p>When you write code in Cpp2, it uses a simpler, safer syntax compared to traditional C++. Cpp2 aims to reduce common C++ pitfalls, introducing features like explicit ownership models, safe pointers, and modern control structures. This Cpp2 code still represents C++ constructs but uses a syntax that encourages safer, more maintainable programming.<\/p>\n\n\n\n<p>2- <strong>CppFront Translation to C++<\/strong>:<\/p>\n\n\n\n<p>CppFront takes this Cpp2 code and translates it into standard C++ code, which then conforms to C++20 or earlier standards. This conversion involves mapping Cpp2 constructs to their equivalent C++ structures, preserving behavior while improving readability and safety. For example, contracts and pattern matching in Cpp2 are reinterpreted in C++ code so that existing C++ compilers can understand them.<\/p>\n\n\n\n<p>3- <strong>Compiling with a Standard C++ Compiler<\/strong>:<\/p>\n\n\n\n<p>Once CppFront has converted the Cpp2 code to C++, you can then compile it using any standard C++ compiler (like GCC, Clang, or MSVC). This ensures compatibility with existing C++ ecosystems, libraries, and tools.<\/p>\n\n\n\n<p>This <strong>source-to-source translation<\/strong> approach allows Cpp2 to be backward compatible with C++ while letting developers incrementally adopt safer syntax, much like TypeScript\u2019s design of compiling to JavaScript.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong>:<\/p>\n\n\n\n<p>TypeScript was in experiment stage in 2006 and in 2012 the first version of TypeScript was officially announced by Microsoft. Today, TypeScript is a key player in the JavaScript ecosystem, offering developers powerful tools to create scalable and maintainable applications. Its growth reflects the broader trends in software development, where type safety and developer productivity are increasingly prioritized.<\/p>\n\n\n\n<p>I hope the story of Cpp2 will mirror that of TypeScript, leading to widespread adoption in a few years to simplify and enhance the safety of C++ development.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript was developed to address several key challenges faced by developers working with JavaScript, particularly in large-scale applications. Indeed, TypeScript is a superset of JavaScript that introduces optional static typing and other features to enhance the development experience, particularly for large-scale applications. Its history reflects the growing need for better tooling and structure in JavaScript &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/cpp2-for-c-serves-a-similar-purpose-as-typescript-does-for-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cpp2 for C++ serves a similar purpose as TypeScript does for JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2151","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=2151"}],"version-history":[{"count":4,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2151\/revisions"}],"predecessor-version":[{"id":2158,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2151\/revisions\/2158"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=2151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=2151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=2151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}