Multi-Paradigm Programming in C++ Function and Class Templates

Multi-Paradigm Programming in C++ Function and Class Templates
Slide Note
Embed
Share

This course covers the implementation of function and class templates in C++. Learn how to utilize templates for generic programming and create flexible code structures. Dive deep into multi-paradigm programming concepts with hands-on exercises and practical examples. Understand the power of templates in enhancing code reusability and efficiency. Gain insights into advanced C++ programming techniques through this comprehensive course.

  • C++
  • Templates
  • Programming
  • Multi-Paradigm
  • Washington University

Uploaded on Mar 01, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. E81 CSE 428S: Multi-Paradigm Programming in C++ Function and Class Templates Department of Computer Science & Engineering Washington University, St. Louis MO Adam Mitz mitzah@wustl.edu 1

  2. Function Templates A function is a formula from which we can generate type-specific versions of a function [LLM pp.652] That formula also imposes requirements on the specific types with which the template may be parameterized E.g., copy construction for arguments passed by value Those requirements define a set of acceptable types Per Liskov Substitution Principle for interface polymorphism I.e., any type that meets the requirements can be plugged in Design considerations for defining function templates Try to impose as few type requirements as is reasonable E.g., passing by const reference instead of by value avoids requiring copy construction of the types that can be passed in E.g., if you can code efficiently using only operator<don t also require the parameterized types to support operator> CSE 428S Multi-Paradigm Programming in C++ 2

  3. Class Templates Similar considerations for interface polymorphism and design of type requirements as function templates But expanded to all requirements member functions impose E.g., copy constructor requires copy initialization of members and default constructor requires default initialization of them Compiler s ability to deduce parameter types of class templates is often less than for function templates So, may need to add further information to class templates E.g., using typename to tell compiler something is a type E.g., giving forward declarations of class templates like template <typename> class Foo; template <typename> class Baz; Friendship relationships involving class templates Can grant friendship for all instantiations or only specific ones E.g., Baz<T> may say friend class Foo<T>; which means that only instantiations with the same type are friends CSE 428S Multi-Paradigm Programming in C++ 3

  4. Default Template Arguments Useful for defining overridable behavior E.g., providing less<T> as the default comparison operator for a function template that also can use others [LLM pp.670] Illustrates the power of making functions, operators, and other callable objects into templates, as the standard libraries do Collecting related types within templates also may motivate having default types for the template E.g., for a class template that declares and defines operators over numeric types, int may be an appropriate default type Then, as long as the operators still are appropriate for them can plug in other types like float, double, etc. If necessary, can specialize definitions of operators for particular types (a topic we will cover in subsequent lectures) CSE 428S Multi-Paradigm Programming in C++ 4

  5. Studio 1 First examines how even non-template functions impose requirements on types they use Then considers how using a function template (e.g., std::swap) may impose additional type requirements Also explores how to declare and use default template arguments, and friendship relationships involving class templates Studios 0 through 11 are due 11:59pm Monday Octomber 16th (night before Exam 0) Submit as soon as each is done so you get feedback and can resubmit any that may be marked incomplete CSE 428S Multi-Paradigm Programming in C++ 5

More Related Content