C++0X New String Literals and User-Defined Literals

C++0X New String Literals and User-Defined Literals

Literals New String Literals User-defined Literals Sources C++0x New String Literals and User-defined Literals Pascal Andreas d'Hermilly Anders Boesen Lindbo Larsen Datalogisk Institut Københavns Universitet April 25, 2008 Literals New String Literals User-defined Literals Sources Literals A literal is a notation for representing a value within source code, e.g.: "hello" (string) 'c' (character) 14 (integer) 3.1 (double) 3.1f (float) We call the f in 3.1f the suffix modifier. The type of a literal is thus determined from its syntactic form. In the following we shall se how the C++ handling of literals is changed with C++0x. Literals New String Literals User-defined Literals Sources String Literals Lots of demands for cross-platform functionality. Current standing for C++: not good, compared to e.g. java or python The language doesn't use a standard character-encoding set for non-ascii, but only provides containers for the bits. Literals New String Literals User-defined Literals Sources Current String Literals String literals is an array consisting of either: char wchar t Examples of current string literals: "A string literal in double quotes" being a const char L"Example" being a const wchar t In C++0x, better support for Unicode will be introduced. Encodings that will be supported: UTF-8, UTF-16, and UTF-32 Literals New String Literals User-defined Literals Sources New String Literals New character types: char16 t char32 t Examples of new string literals: u8"UTF-8 string" u"UTF-16 string" U"UTF-32 string" using const char[], const char16[], and const char32[] respectively. Also inline insertion of unicode-codepoint will be available: u8"Unicode character: nu2018 " The code that follows nu is hex for a 16 bit Unicode codepoint. 32 bit, use nU Literals New String Literals User-defined Literals Sources Raw Strings Raw strings allow you to create strings without having to escape special characters. Examples: R"[ls /home/pascal/ | grep ".pdf" ]" R"delim[ls /home/pascal/ | grep ".pdf" ]delim" Literals New String Literals User-defined Literals Sources User-defined Literals The current problem C++ recognizes literals only for the built-in data types, e.g.: integers, strings and booleans. These types are not sufficient to maintain C compatibility! (C99 has introduced new data types, that requires recognition of new literals) The goal of C++0x Make it possible to define new kinds of literal modifiers that will construct user-defined objects. C compatibility should be maintained as far as possible Literals New String Literals User-defined Literals Sources Examples of User-defined Literals Some of the possibilities with user-defined literals: "hello"s (std::string) 3.5i (imaginary type for complex numbers) 10011011b (binary literals) 1234567890123456789xxl (arbitrary range/precision) Notice the use of suffixes to identify the literal type. Literals New String Literals User-defined Literals Sources Proposed Solution Every user-defined literal is interpreted as a call to a new kind of operator (called literal operator): X operator "suffix" (parameters ); where X is the return type of the newly defined literal with the suffix suffix. parameters decides in which format the literal operator recieves the value of the literal. For example, the literal 3.5i will perform the following call: operator "i" (parameters ); Literals New String Literals User-defined Literals Sources Examples of Literal Operators A cooked-form literal operator can have the following form: X operator "suffix" (unsigned int); For example: cm t operator "cm" (unsigned int); cm t n = 45cm; In this case, 45 is given as parameter to the literal operator. A raw-form literal operator can have the following form: X operator "suffix" (char const*); Example: long number operator "xxl" (const char*); long number n = 123456789012345xxl; In this case, the null-terminated C string "123456789012345" is given as parameter to the literal operator. Literals New String Literals User-defined Literals Sources Sources The C++ Standards Committee, n2378 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2378.pdf The C++ Standards Committee, n2442 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2442.htm Wikipedia, C++0x http://en.wikipedia.org/wiki/C++0x.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us