
Understanding Variadic Templates and Parameter Packs in C++
Explore the concept of variadic templates and parameter packs in C++, enabling the handling of varying numbers of parameters and types in a generic and efficient manner. Learn how to manage elements, apply patterns, and forward parameter packs effectively to enhance your programming skills.
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
E81 CSE 428S: Multi-Paradigm Programming in C++ Parameter Packs and Variadic Templates Department of Computer Science & Engineering Washington University, St. Louis MO Chris Gill cdgill@wustl.edu 1
Variadic Templates Parameter Packs A variadic template has varying numbers of parameters An ellipsis (...) indicates the portion that may vary Those features may be useful for printing values, calling functions, etc. in a highly generic manner Function templates also may have varying numbers of function parameters as well as of type parameters So, all of the overloads to print out an arbitrary sequence of types to a stream could be passed as function arguments Variadic function templates are often written recursively with a non-variadic version of the function template used to halt it A non-variadic version is by definition more specialized than a variadic version and so will match better (if it is in scope) Lists of varying parameters are called parameter packs Template parameter packs specify varying template parameters Function parameter packs specify varying function parameters E.g., template <typename T, typename... TemplatePack> T foo (const TemplatePack& ... function_pack); Operator sizeof... returns how many elements are in a pack CSE 428S Multi-Paradigm Programming in C++ 2
Parameter Pack Expansion and Forwarding Parameter pack expansions help manage the elements E.g., a type pattern (e.g., reference to const) can be applied as in R foo (const T& t, const TpltPack& ... fnc_pack); E.g., arguments can be passed to a subsequent recursive invocation of the function as in return foo (fnc_pack...); Can also apply a function, a type transformation, or other more sophisticated patterns to each element in the pack, e.g., print (os, bytecount(rest)...); // count bytes passed Can forward parameters packs to preserve their format This can help to avoid accidental conversions due to template argument deduction, and/or implicit type transformations This is especially important for templates that mix rvalue/lvalue references, and/or use type transformation library templates E.g., when declaring and defining functionality like a new container type with emplace_back, etc. CSE 428S Multi-Paradigm Programming in C++ 3
Studio 15 Looks at how variadic function templates can be declared and defined recursively Uses variadic function templates to print out arbitrary lists of parameters of arbitrary types Explores how patterns can be applied to elements of parameter packs when they are expanded, e.g., to collect metadata like how many bytes they contain Studios 12 through 19 are due 11:59pm Wednesday November 29th (night before Exam 1) 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++ 4