Learn what a template variable is and how it works. Also, discover the benefit of a template variable and when you should use it.
- 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 digits…of our pi correctly because I'm using a long double.…
Author
Released
12/20/2018- Template fundamentals
- STL containers
- Accessing iterators
- Output, forward, and bidirectional iterators
- The transform function
- Transforming strings and types
- Arithmetic, relational, and logical functions
- STL algorithms
Skill Level Intermediate
Duration
Views
Related Courses
-
Learning C++
with Erin Colvin2h 26m Beginner -
Learning C++ Pointers
with Peggy Fisher53m 55s Intermediate
-
Introduction
-
1. Templates
-
What is a template?4m 43s
-
Template syntax1m 48s
-
Template specialization1m 49s
-
Template variables4m 14s
-
Impact on header files3m 13s
-
Type inference6m 45s
-
Argument deduction3m 15s
-
-
2. STL Containers
-
3. STL Iterators
-
Accessing iterators2m 28s
-
Input iterators2m 2s
-
Output iterators1m 27s
-
Forward iterators1m 58s
-
Bidirectional iterators1m 59s
-
Random access iterators2m 2s
-
-
4. Transformations
-
The transform function5m 28s
-
Lambda transformations4m 23s
-
Transforming strings2m 49s
-
Binary transformations3m 7s
-
Transforming types3m 3s
-
-
5. STL Functions
-
About functors1m 46s
-
Arithmetic functors2m 10s
-
Relational functors3m 13s
-
Logical functors1m 31s
-
-
6. STL Algorithm
-
About STL algorithms1m 42s
-
Testing conditions2m 36s
-
Searching and counting4m 38s
-
Replacing and removing4m 14s
-
Modifying algorithms5m 46s
-
Partitions2m 52s
-
Sorting3m 42s
-
Merging sequences2m 4s
-
Binary searches3m 57s
-
-
Conclusion
-
Goodbye1m 29s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Template variables