From the course: C++ Templates and the STL

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Template variables

Template variables

- C++ 14 provides a new template implementation for strongly typed variables. Here I have a working copy of template-variable.cpp from Chapter one of the exercise files, and this will only work in C++ 14 or later. Here we have a template variable. We have our normal template syntax followed by a variable of type T, and in this case we're defining pi to 20 places. Down here we're setting cout to use a precision of 20 places for our floating point variables. And we are using this variable with the double type. So when I build and run this, of course double does not have 20 places of precision, after 16 places or so it starts to go funny. Now if I change this to long double, now the long double type on this machine uses an 80 bit float, which will provide the full 20 digits of precision. On many systems, including Microsoft Windows, it still uses a 64 bit flow which will not be any different than double. But here on this system when I build and run, you notice that I'm getting all 20…

Contents