{"id":193,"date":"2016-03-27T04:14:14","date_gmt":"2016-03-27T04:14:14","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=193"},"modified":"2016-03-27T04:14:14","modified_gmt":"2016-03-27T04:14:14","slug":"user-defined-literals-in-c","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2016\/03\/27\/user-defined-literals-in-c\/","title":{"rendered":"User-defined Literals in C++"},"content":{"rendered":"<p>C++11 introduced <em><strong>user-defined literals<\/strong><\/em>, allowing programmers to define special suffixes that can be associated with the built-in literal types: character, integer, float, boolean, and pointer. When designed and used properly, these provide nice syntactic sugar facilitating readability and at the same time, increasing type safety.<\/p>\n<p>For example, I can define an operator <em><strong>_kg<\/strong><\/em>\u00a0that can be suffixed to a float literal to explicitly denote that is a weight expressed in Kilograms. Likewise, I can define an operator for <em><strong>_gm<\/strong><\/em> for weight that needs to be expressed in Grams.<\/p>\n<p style=\"padding-left: 30px;\"><strong>long double operator \u201c\u201d _gm (long double val) { return val; }<\/strong><\/p>\n<p style=\"padding-left: 30px;\"><strong>long double operator \u201c\u201d _kg (long double val) { return val * 1000; }<\/strong><\/p>\n<p>These can be used as follows:<\/p>\n<p style=\"padding-left: 30px;\"><strong>long double total_weight = 1.5_kg + 25.7_gm;<\/strong><\/p>\n<p>The leading underscore is mandatory, except for the Standard Library, which can define suffixes without the underscore. In C++14, the Standard Library uses this facility.<\/p>\n<p>For example,<\/p>\n<p style=\"padding-left: 30px;\"><strong>auto my_string = \u201chello\u201ds;<\/strong><\/p>\n<p>Here the inferred type of the variable my_string will be the <em><strong>string<\/strong><\/em>\u00a0type because of the <em><strong>s<\/strong><\/em>\u00a0suffix. Note that I\u00a0did not say <em><strong>\u201chello\u201d_s<\/strong><\/em>.<\/p>\n<p>You can read more about this feature <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/user_literal\" target=\"_blank\">here<\/a>.<\/p>\n<p>In today\u2019s post, I want to share an interesting use case of this feature. I have been interested in algorithmic music composition for a while, and I have shared some of my experiments in previous blog posts (for example, <a href=\"http:\/\/www.rangakrish.com\/index.php\/2016\/03\/12\/converting-text-to-music\/\" target=\"_blank\">this one<\/a>\u00a0). I use multiple language environments for my research including Lisp, Mathematica, C++, and Java. So my example for today\u2019s discussion is from music, particularly, expressing note sequences as a user-defined literal.<\/p>\n<p>In MIDI, the note <em><strong>C4<\/strong><\/em>, that is the <em><strong>C<\/strong><\/em>\u00a0in 4th octave is usually given the MIDI value of <em><strong>60<\/strong><\/em>. There are 12 elements in each octave, so <em><strong>C5<\/strong><\/em> will be <em><strong>72<\/strong><\/em>, and so on. Wouldn\u2019t it be nice if the programmer can write a MIDI note string as <em><strong>\u201cC#3DE4BbF\u201d_midi<\/strong><\/em>, which gets converted to an array of corresponding integers? This will make the program more readable, even without elaborate comments explaining the cryptic string. In the example, the string must evaluate to <em><strong>{49, 62, 64, 70, 65}<\/strong><\/em>.<\/p>\n<p>Here is a C++ program to model this user-defined literal.<\/p>\n<figure id=\"attachment_194\" aria-describedby=\"caption-attachment-194\" style=\"width: 649px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/User-defined-Literal.png\" rel=\"attachment wp-att-194\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"194\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2016\/03\/27\/user-defined-literals-in-c\/user-defined-literal\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/User-defined-Literal.png\" data-orig-size=\"649,1004\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"User-defined Literal\" data-image-description=\"&lt;p&gt;User-defined Literal&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;User-defined Literal&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/User-defined-Literal.png\" class=\"wp-image-194 size-full\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/User-defined-Literal.png?resize=649%2C1004\" alt=\"User-defined Literal\" width=\"649\" height=\"1004\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/User-defined-Literal.png?w=649&amp;ssl=1 649w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/03\/User-defined-Literal.png?resize=194%2C300&amp;ssl=1 194w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/a><figcaption id=\"caption-attachment-194\" class=\"wp-caption-text\">User-defined Literal<\/figcaption><\/figure>\n<p>I wrote and tested this program in <em><strong>Xcode 7.2.1<\/strong><\/em> on my iMac. You can also see liberal uses of the <em><strong>auto<\/strong><\/em>\u00a0keyword (after C++11, I have become addicted to it as it saves many keystrokes and makes the program more readable!)<\/p>\n<p>You can download the program <a href=\"http:\/\/www.rangakrish.com\/downloads\/UserLiteral.cpp\" target=\"_blank\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++11 introduced user-defined literals, allowing programmers to define special suffixes that can be associated with the built-in literal types: character, integer, float, boolean, and pointer. When designed and used properly, these provide nice syntactic sugar facilitating readability and at the same time, increasing type safety. For example, I can define an operator _kg\u00a0that can be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[49,5,17],"tags":[38],"class_list":["post-193","post","type-post","status-publish","format-standard","hentry","category-c","category-music","category-programming","tag-midi"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-37","jetpack-related-posts":[{"id":666,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/10\/21\/c17-initalization-in-selection-statements\/","url_meta":{"origin":193,"position":0},"title":"C++17: Initialization in Selection Statements","author":"admin","date":"October 21, 2017","format":false,"excerpt":"C++17 enhances if and switch statements with the ability to define\u00a0 variables whose life-time is limited to the corresponding scope. This is in keeping with the general guideline that variables should have a tight scope, i.e.,\u00a0 should be defined as close to the point of use as possible and should\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Sample Class","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/10\/classdefn.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2975,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/","url_meta":{"origin":193,"position":1},"title":"Std::tie","author":"admin","date":"December 25, 2022","format":false,"excerpt":"std::tuple is a widely used abstraction in C++ and has been around since C++11. It is a generalization of std::pair. std::tie is convenient when we want to create a tuple of lvalue references to existing variables. It is a function template commonly used to unpack a tuple into individual objects.\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::tie Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2593,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/11\/01\/implicit-indexing-and-slicing-in-newlisp\/","url_meta":{"origin":193,"position":2},"title":"Implicit Indexing and Slicing in &#8220;newLisp&#8221;","author":"admin","date":"November 1, 2021","format":false,"excerpt":"In traditional Lisp (Common Lisp), when evaluating an S-expression list, the first element in the \"functor\" position must be a function or valid operator. newLisp\u00a0relaxes this requirement and allows the first element to be a context symbol type, a list, an array, or an integer. For today's discussion, let us\u2026","rel":"","context":"In &quot;newLisp&quot;","block_context":{"text":"newLisp","link":"https:\/\/www.rangakrish.com\/index.php\/category\/newlisp\/"},"img":{"alt_text":"List Indexing","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/11\/list1-300x141.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":193,"position":3},"title":"std::is_scoped_enum","author":"admin","date":"August 13, 2023","format":false,"excerpt":"The type trait \"std::is_scoped_enum<T>::value\" was introduced in C++23 to check whether the type \u201cT\u201d is a scoped enum type. Another way to use this is std::is_scoped_enum_v<T>. Before getting into this trait in detail, let us briefly recap the differences between unscoped and scoped enums. Unscoped Enums Unscoped enums are the\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Unscoped Enums","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3675,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/04\/24\/interfaces-without-inheritance-comparing-c-and-common-lisp\/","url_meta":{"origin":193,"position":4},"title":"Interfaces Without Inheritance: Comparing C++ and Common Lisp","author":"admin","date":"April 24, 2025","format":false,"excerpt":"Clean interface design is a crucial aspect of software engineering since it enables code flexibility, reuse, and maintainability. Developers who prefer an object-oriented approach typically rely on inheritance to define the interface and thus establish type relationships. While this can lead to a good design if approached carefully, detractors of\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Rectangle and Circle Defined","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/04\/cpp2-251x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2057,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/","url_meta":{"origin":193,"position":5},"title":"Calling Lisp Functions from Elixir","author":"admin","date":"July 19, 2020","format":false,"excerpt":"In the last article\u00a0I showed how we can simulate the idea of Lisp's \"closure\" in Elixir. Today, I would like to demonstrate how we can call Lisp functions from Elixir using the NIF interface. What is the need to integrate Elixir with another language? I can think of two reasons:\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Using the Lisp Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}