Function and class templates, specialization, variadic templates, type traits, SFINAE and C++20 concepts.
39 cards · basic cards · AI-written, checked twice. Edit anything.
- What does a function template allow you to do?
Show answer
Define a function that works with multiple data types by using type parameters instead of specific types.
- How do you declare a function template?
Show answer
Use the template keyword followed by angle brackets containing type parameters: template<typename T> returnType functionName(T param) { }
- What is template instantiation?
Show answer
The process of creating a concrete function or class from a template by substituting actual types for template parameters.
- What is template argument deduction?
Show answer
The compiler automatically determines what template arguments to use based on the function arguments passed at the call site.
- What are explicit template arguments?
Show answer
Template arguments you specify directly in angle brackets when calling a function template, overriding automatic deduction.
- What is a class template?
Show answer
A blueprint for creating classes that work with different data types, declared with the template keyword and type parameters.
- How do you define a member function of a class template outside the class?
Show answer
Use template<typename T> and scope resolution: template<typename T> ReturnType ClassName<T>::memberName(params) { }
- What is a template member function?
Show answer
A member function inside a class (template or not) that is itself a template with its own type parameters.
- What is full template specialization?
Show answer
A complete specialization where all template parameters are replaced with concrete types, providing a custom implementation for specific types.
- What is partial template specialization?
Show answer
A specialization where some template parameters remain generic while others are constrained, allowing matching multiple related types.
- Can you partially specialize function templates?
Show answer
No, function templates do not support partial specialization, only full specialization. Use overloading as an alternative.
- How does template specialization affect overload resolution?
Show answer
More specialized versions are preferred over general templates. Full specialization is preferred over partial, and partial over the general template.
- What is a variadic template parameter pack?
Show answer
A template parameter that can accept a variable number of arguments, declared as typename... Args or class... Args.
- How do you expand a parameter pack?
Show answer
Use the pack name followed by an ellipsis in a context where expansion makes sense, such as in function arguments or template arguments.
- What is a base case in a variadic template?
Show answer
A specialization or overload that terminates recursion by handling zero arguments, preventing infinite instantiation.