Skip to Content
Mastering C++ Programming
book

Mastering C++ Programming

by Jeganathan Swaminathan
September 2017
Beginner to intermediate
384 pages
8h 4m
English
Packt Publishing
Content preview from Mastering C++ Programming

Template type auto-deduction for class templates

I'm sure you will love what you are about to see in the sample code.  Though templates are quite useful, a lot of people don't like it due to its tough and weird syntax. But you don't have to worry anymore; take a look at the following code snippet:

#include <iostream>using namespace std;template <typename T1, typename T2>class MyClass {     private:          T1 t1;          T2 t2;     public:          MyClass( T1 t1 = T1(), T2 t2 = T2() ) { }          void printSizeOfDataTypes() {               cout << "\nSize of t1 is " << sizeof ( t1 ) << " bytes." << endl;               cout << "\nSize of t2 is " << sizeof ( t2 ) << " bytes." << endl;     }};int main ( ) {    //Until C++14    MyClass<int, double> obj1;    obj1.printSizeOfDataTypes( );    //New syntax in C++17 MyClass obj2( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Multi-Paradigm Programming with Modern C++

Multi-Paradigm Programming with Modern C++

Georgy Pashkov

Publisher Resources

ISBN: 9781786461629Supplemental Content