Multi-Paradigm Programming in C++ Template Argument Deduction

Multi-Paradigm Programming in C++ Template Argument Deduction
Slide Note
Embed
Share

"Compiler utilizes arguments in function templates to determine parameter types and allow for type conversions. Learn about template type parameter conversions, function template explicit arguments, trailing returns and type transformation, and function pointers and references in multi-paradigm programming with C++."

  • C++
  • Template
  • Argument Deduction
  • Type Conversion
  • Programming

Uploaded on Mar 03, 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++ Template Argument Deduction Department of Computer Science & Engineering Washington University, St. Louis MO Adam Mitz mitzah@wustl.edu 1

  2. Template Type Parameter Conversions The compiler uses the arguments in a call to a function template to determine the parameter types May be able to establish an exact match E.g., int i = 7; int j = 2; std::swap(i, j); Can also exploit some specific type conversions From a pointer (or reference) to a non-const variable to a pointer (or reference) to a const version of that type An array type to pointer-to-array-element type, or a function type to the corresponding pointer-to-function type Having two different template parameter types may help Allows different types (e.g., int, long) to be compared, etc. Then, normal conversions can apply to those types if either type can be converted to the other type CSE 428S Multi-Paradigm Programming in C++ 2

  3. Function Template Explicit Arguments Sometimes the template argument times cannot be deduced directly from the types of the arguments E.g., if the return type differs from the argument types Can provide explicit arguments at the point of call Via an angle-bracket-delimited list after the function name: int i=1; float f=2.0; auto d=myfunc<double>(i,f); Normal conversions also apply for explicit arguments E.g., an enumerated type to int, int to long, etc. Can force which conversion happens via an explicit argument CSE 428S Multi-Paradigm Programming in C++ 3

  4. Trailing Returns and Type Transformation Can use a trailing type declaration to use the arguments types in a decltype expression Especially useful for classes that behave like pointers E.g., to return the type that is aliased by an iterator Type transformation library template classes Defined in the <type_traits> library header file Can obtain the type a pointer, reference, or iterator aliases Can obtain a pointer or reference type to a type Can add const to a non-const type Can convert an unsigned type into a signed type Can convert a signed type into an unsigned type Can remove one ore more indexing dimensions from an array CSE 428S Multi-Paradigm Programming in C++ 4

  5. Function Pointers and References Can deduce template arguments from function pointer If there is a unique match that will happen automatically Or, give explicit template arguments for overloaded signatures Also can deduce template arguments from references For lvalue references (T&), constness is same as the argument For rvalue references (T&&), non-const type is deduced from an rvalue argument since rvalues are already inherently const References to references collapse to just references: T& &, T&& &, and T& && all collapse to T&; T&& && collapses to T&& Watch out for unexpected effects for rvalue references It is often best to provide an overload that takes an rvalue reference and another that takes a reference to a const lvalue CSE 428S Multi-Paradigm Programming in C++ 5

  6. Implementation of std::move (per LLM) // based on code example from LLM pp. 690 (added typename) template <typename T> typename remove_reference<T>::type&& moveit (T&& t) { return static_cast<typename remove_reference<T>::type&&> (t); } If an lvalue is passed in, move is instantiated to take a reference to an lvalue and return an rvalue reference to it (ok to static_cast lvalue to rvalue reference) Which then means the lvalue s implementation may be stolen by a move constructor or move assignment operator Generally not safe to use the lvalue after that until it has been re- assigned a value (which gives it a valid implementation) Otherwise, if an rvalue is passed to move, it is instantiated to take and return an rvalue reference (the static_cast does nothing) CSE 428S Multi-Paradigm Programming in C++ 6

  7. Forwarding and Template Argument Types Some template instantiations may lose key type information about the arguments they re called with Losing references, e.g., int& becomes int (passed by value) Losing constness, e.g., const int becomes int The std::forward function template lets us preserve all type information about the arguments being used Must provide it explicit template arguments E.g., std::forward<T> (arg); // arg is of type T&& An rvalue reference to the template parameter type is passed to std::forward, so reference collapsing preserves type info CSE 428S Multi-Paradigm Programming in C++ 7

  8. Studio 13 Looks at how type conversions are used to implement the std::move function template Uses the make_unique function template instead of using the new operator directly Explores additional type conversions provided by templates in the type_traits library and uses typeid and type_info to examine their effects Studios 12 through 19 are due 11:59pm Monday November 25th (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++ 8

Related


More Related Content