Home > Articles

Safety in Numbers: Introducing C++14's Binary Literals, Digit Separators, and Variable Templates

  • Print
  • + Share This
  • 💬 Discuss
Like this article? We recommend
Danny Kalev highlights three new core features in C++14 that simplify the design and implementation of numeric apps. Binary literals such as 0b10101 enable you to parse binary numbers without the onus of converting them to decimal; the new digit separator improves code readability; and, finally, variable templates let you define parameterized constants instead of resorting to cumbersome static member functions.

C++14 isn't revolutionary, nor was it meant to be. It mostly consists of bug fixes and small conveniences that should please C++ programmers while improving the performance, reliability, and portability of new code. This article presents three new numeric-related features that have now made it into the standard: binary literals, the single quotation-mark (') digit separator, and variable templates.

Binary Literals

In addition to decimal values, C++ has always supported octal literals:

int fifteen= 017;// equivalent to 15 decimal

as well as hexadecimal values:

unsigned char ASCII_letter= 0xfc; //equivalent to 252 decimal

However, until recently, C++ didn't support binary literals such as 11111100; you had to convert them to one of the supported bases before using them in C++. This asymmetry between binary literals and literals in other bases has finally been rectified. You can now use binary literals directly in C++14 code like this:

//C++14
int x= 0b11111100;
if (var==0b101)
  //...
switch (binliteral)
{
case 0B100:
       //...
       break;
case 0B101:
       //...
       break;
//...
}

A binary literal begins with the prefix 0b or 0B followed by a sequence of one or more binary digits, namely 0 and 1. This syntax is borrowed from other languages such as D, Java, Perl, and Python, all of which support binary literals.

The type of a binary literal is int. You can use a binary literal wherever a constant integral value is expected:

enum class Bits //C++14
{
 CLEAR = 0b0,
 SET =0b1
};

At this point, I'll bet you want to ask whether the Standard Library supports binary literals as well. In other words:

  • Do iostream objects recognize the std::bin manipulator?
  • Will fstream objects know how to format binary literals when writing them to files?
  • Will stringstream objects know how to parse a string containing 0b1010, for example?

Regrettably, the answer to all of these questions is "No." Adding core-language support for binary literals consists of changing the parsing rules of C++ (a relatively trivial task). Adding Standard Library support is a complex and time-consuming chore that couldn't have been completed prior to the C++14 Final Committee Draft (FCD) milestone. Consequently, vendors are likely to ship Standard Library implementations that support binary literals voluntarily; I'm afraid an official ISO endorsement is unlikely before C++17.

Quotation Mark as Digit Separator

Quotation Mark as Digit Separator

Another long-awaited small convenience is a digit separator. Certain programming languages allow you to write numbers with separators to improve readability. Consider the following Ada snippet:

--an Ada statement with digit separators
My_Savings := My_Savings * 1_000_000.0;

An Ada compiler ignores the underscores when parsing the token 1_000_000.0. Several proposals suggested adding a similar feature to C++. However, the comma (,) and underscore (_) were ruled out because they might lead to parsing ambiguities. For example, if commas were allowed as digit separators, the following function call would be ambiguous:

func(10,0); //two arguments or 100?

An underscore might lead to similar parsing ambiguities:

int _100_; //not your typical variable name, yet valid
func(_100_); //is it a variable, or the literal 100?

Therefore, the standards committee decided to accept the single quotation mark as a digit separator:

//C++14. All of the following variables equal 1048576
long decval=1'048'576;  //groups of three digits
long hexval=0x10'0000;// four digits
long octval=00'04'00'00'00;//two digits
long binval=0b100'000000'000000'000000;//six digits

The original proposal for adding a single quotation mark as a digit separator for C++ is available online.

Variable Templates

Variable Templates

C++ programmers are used to only two types of templates, namely class templates and functions templates; it's hard to imagine why variable templates would be needed. However, when you examine some of the more prevalent C++ design idioms, including meta-functions and constexpr static member functions that merely return a const static data member, you realize that these are workarounds for parameterized constants. This line of reasoning led the C++ designers to introduce variable templates to C++14. (The original variable templates proposal is available online.) The canonical example uses the constant pi, whose actual type depends on the specialization used:

//C++14, a variable template
template<typename T>
  constexpr T pi = T(3.1415926535897932385);

You access pi as you would an ordinary template: pi<double>, pi <long double>, or pi<T>. A C++14 function template that calculates a circular area can use pi like this:

//C++14, a variable template in use
template<typename T>
T circular_area(T r) {
  return pi<T> * r * r;
}
double darea= circular_area(5.5);//uses pi<double>
float farea= circular_area(5.5f);//uses pi<float>

Template variables aren't confined to built-in types. They can be user-defined types as well, such as matrix<T>, complex<T>, and so on.

Conclusion

Conclusion

The C++14 FCD was approved at the Issaquah meeting in February 2014. (An up-to-date overview of the C++14 features is available online.) Surprisingly, several features that had made it into the Committee Draft and seemed like sure bets were eventually dropped from the FCD for various reasons. These include the std::dynarray container, runtime-sized arrays (also known as Variable Length Arrays in C parlance) and even a filesystem library. Adhering to the features-will-yield-to-schedule principle, it looks like C++14 will roll out on time, albeit with fewer features than originally expected.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus