From c80f3b6251faaa2690ca5ffb5a81c6f9dddb17b6 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Thu, 3 Nov 2016 21:13:04 -0700 Subject: [PATCH] User-defined literals. --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 21fd1dd..b81bbd3 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,9 @@ C++14 includes the following new language features: - [variadic templates](#variadic-templates) - [relaxing constraints on constexpr functions](#relaxing-constraints-on-constexpr-functions) +C++14 includes the following new library features: +- [user-defined literals for standard library types](#user-defined-literals-for-standard-library-types) + ## C++17 Language Features ### Template argument deduction for class templates @@ -423,7 +426,23 @@ factorial(5); // == 120 ``` ## C++14 Library Features -TODO + +### User-defined literals for standard library types +New user-defined literals for standard library types, including new built-in literals for `chrono` and `basic_string`. These can be `constexpr` meaning they can be used at compile-time. Some uses for these literals include compile-time integer parsing, binary literals, and imaginary number literals. +```c++ +constexpr int operator"" _test(unsigned long long x) { + return 0; +} +123_test; // == 0 +``` + +Built in literals for `chrono`: +```c++ +using namespace std::chrono_literals; +auto day = 24h; +day.count(); // == 24 +std::chrono::duration_cast(day).count(); // == 1440 +``` ## C++11 Language Features TODO