Title: Theory / Unimportant / CS / Programming / C++ / Manual / Norm / 23 Templates
Tags: declaration todo_declaration automatedNewLineAfterCommaSpace syntactical_sugar

Question: What is the best practice to develop a template class/function
Answer: Develop the function for a specific type. Debug and test it. Then add the template parameter
Page: 670
Chapter: 23
Section: 2.1
Context: C++

Name: Template instantiation
Definition: The process of generating a class or a function from a template plus a template argument list
Page: 671
Chapter: 23
Section: 2.1
Context: C++

Name: A concept
Definition: A predicate that a type can satisfy or not
Page: 672
Chapter: 23
Section: 3
Context: C++
Example: being subscriptable
Example2: inheriting from BaseClass

Name: An associated type
Definition: A type which is declared as a member of a class
Page: 677
Chapter: 23
Section: 4.3
Context: C++
Intuition: It's definition usually uses a type parameter. Allow to access this parametrized type from outside the class

Question: In template class, how to access parametrized type of this class from the outside
Answer: uses associated types: i.e.
using type_name = parametrized_type<T>
Page: 677
Chapter: 23
Section: 4.3
Context: C++

Question: constraint on static member of struct/class
Answer: The value should be defined as const in the struct or assigned exactly once in the code
Page: 677
Chapter: 23
Section: 4.3
Context: C++

Name: A member template
Definition: A member that is defined as a template
Page: 678
Chapter: 23
Section: 4.6
Context: C++

Question: Why can't we have a virtual template member
Answer: the vtable implementation assumes there is a fixed number of methods. Template breaks this assumption
Page: 679
Chapter: 23
Section: 4.6.2
Context: C++

Question: How to create a class Implementation that can be accessed only from a class PrincipalClass
Answer: create Implementation as private member of PrincipalClass
Page: 680
Chapter: 23
Section: 4.6.2
Context: C++
Answer2: create Implementation with everythnig private, even constructor, and being a friend of PrincipalClass (its principal advantage being that Implementation don't depends on the template of PrincipalCLass)

Question: In a function call, how to determine which function is called
Answer: Find all function and function template that may be used. If none, it's an error
Page: 690
Chapter: 23
Section: 4.6.2
Context: C++
Answer2: for each pair of template function, keep the more specialized one
Answer3: do standard overload resolution; where template argument can't lead to promotion
Answer4: in case of equality, uses the function instead of the function template
Answer5: if there remain multiple function, it is ambiguous and an error

Name: Substitution Failure Is Not An
Error
Page: 692
Chapter: 23
Section: 4.6.2
Context: C++
Abbreviation: SFINAE

Name: lifting
Definition: Rewritting a function such that it works on more generic type
Page: 701
Chapter: 23
Section: 4.6.2
Context: C++

Instruction: static_assert(
  pred,
  "error message"
)
Page: 709
Effect: if pred can not be evaluated to true at compile time, fail with "error message"
Language: C++
Chapter: 24
Section: 4

Question: What are the possible type that a template can take
Answer: Type parameters: equivalently typename or class
Page: 722
Chapter: 25
Section: 2.2
Context: C++
Answer2: Value parameters of built-in types, enum, pointers to function
Answer3: Template parameter

Question: What are the possible value of a value parameter
Answer: Integral constant expression
Page: 724
Chapter: 25
Section: 2.2
Context: C++
Answer2: Pointer or reference to an object or a function with external linkage
Answer3: a nonoverloaded pointer to a member
Answer4: a null pointer

Instruction: template<foo>
template<bar>
Language: C++
Chapter: 25
Section: 2.2
Abbreviation: template<foo, bar>

Instruction: template<typename T> or template<class T>
Meaning: the next definition can be used with T any arbitrary type.
Language: C++
Chapter: 25
Section: 2.2

Instruction: template<integral_type value>
Meaning: the next definition can be used with value any value of integral_type. The value is fixed at compilation time and can not be modified
Language: C++
Chapter: 25
Section: 2.2

Instruction: template<template<typename> class C>
Meaning: the next definition can be used with C being an arbitrary template
Language: C++
Chapter: 25
Section: 2.2

Instruction: template<meta_type argument = default_value>
Meaning: Same as template<meta_type argument>. Furthermore, if the value currently defined is called without expliciting a value for argument, then default_value is used
Language: C++
Chapter: 25
Section: 2.2

Instruction: template<>
class C<some_type> {
   some_type_implementation
}
Page: 731
Meaning: when C is used with some_type, uses some_type_implementation
Language: C++
Chapter: 25
Section: 3
Name: A specialization of C
Variables: template<typename T>
class C {
   general_implementation
}
Example: vector of boolean is a specialization, where 8 bools are stored in a single byte

Question: How to avoid bloating of the program size and ensure that all uses of C with a pointer type uses the same implementation
Answer: template<>
class C<void*> {
  general_implementation with T replaced by void*
}
Page: 731
Chapter: 25
Section: 3
Variables: templat<typename T>
class C {
   general_implementation
}
Context: C++
Answer2: template<typename T>
class C<T*> : private C<void*> {
  using Base = C<void*>
  with constructor sending everything to base as is
  Returning T* instead of void*, using reinterpret_cast<T*&>(actual T* value)
}

Name: A complete specialization
Definition: A specilaization that fixes all parameters
Page: 731
Chapter: 25
Section: 3
Variables: templat<typename T>
class C {
   general_implementation
}
Context: C++

Name: A partial specialization
Definition: A specilaization that is also a template
Page: 731
Chapter: 25
Section: 3
Variables: templat<typename T>
class C {
   general_implementation
}
Context: C++
Example: Having a special implementation of C when T is a pointer

Question: Standard example of specialization
Answer: Have a special implementation for reference type and for pointer type
Page: 731-733
Chapter: 25
Section: 3
Context: C++
Answer2: general template takes argument by reference; but specialize for small type (e.g. integral, char, small structures) to take them by value
Answer3: for template with unsigned int N argument defined by recursion, special case for N=0

Name: A primary template
Definition: A template which is not a specialization
Page: 735
Chapter: 25
Section: 3.2
Context: C++

Name: S1 is more specialized than S2
Definition: every argument list that matches S1 matches S2
Page: 735
Chapter: 25
Section: 3.2
Variables: template<typename T>
class C {
   general_implementation
}
S1 and S2 two specialization of C
Context: C++

Question: How to have function overloading depending on the return type
Answer: Uses specialization, with return type in a template
Page: 738
Chapter: 25
Section: 3.4.2
Context: C++

Name: Template instantiation
Definition: The process of generating class/function from template class/function
Page: 742
Chapter: 26
Section: 2
Context: C++

Name: A generated specializations
Definition: A specialization of a template generated by replacing a template argument by a value
Page: 742
Chapter: 26
Section: 2
Context: C++

Name: A user-defined specializations
Definition: A specialization of a template explicitly generated by a user
Page: 742
Chapter: 26
Section: 2
Context: C++

Question: What are the instantiation of template function done by the compiler
Answer: The compiler instantiate function that are called
Page: 742
Chapter: 26
Section: 2
Context: C++
Answer2: The compiler instantiate functions whose adress is taken

Instruction: template templated_function_or_class<values>
Page: 742
Meaning: force the compiler to instantiate templated_function_or_class with parameters values
Language: C++
Chapter: 26
Section: 2.2
Name: Explicit instantiation

Instruction: extern template templated_function_or_class<values>
Page: 742
Meaning: Do not implicitly instantiate this templated_function_or_class
Language: C++
Chapter: 26
Section: 2.2
Intuition: A user-defined instantiation is somewhere

Name: Name binding
Definition: The process of finding the declaration for each name explicitly or implicitly used in a template
Page: 746
Chapter: 26
Section: 2.2

Name: Dependend names
Definition: names that depend on a template parameter.
Page: 746
Chapter: 26
Section: 2.2
Example: the instantiation of + depends on T
Applied to: bool tracing;
template<typename T>
T sum(std::vector<T>& v)
{
  T t {};
  if (tracing)
    cerr << "sum(" << &v << ")\n";
  for (int i = 0; i!=v.siz e(); i++)
    t = t + v[i];
  return t;
}

Name: Nondependent names
Definition: A name which is not dependent
Page: 746
Chapter: 26
Section: 2.2
Example: the instantiation of vectors does not depends on T
Applied to: bool tracing;
template<typename T>
T sum(std::vector<T>& v)
{
  T t {};
  if (tracing)
    cerr << "sum(" << &v << ")\n";
  for (int i = 0; i!=v.siz e(); i++)
    t = t + v[i];
  return t;
}

Instruction: object.template name<type>
Page: 748
Effect: access to object.name, where <type> is the argument of the template name
Language: C++
Chapter: 26
Section: 3.1

Name: Point-of-definition
Definition: The moment in the compilation where the compiler read the definition
Page: 748
Chapter: 26
Section: 3.2
Context: C++
Intuition: for template, it is the time the compiler consider the parameterized class/function

Name: Point-of-instantiation of an instance I of C<some_type>
Definition: The nearest global or namespace scope enclosing the instance I of C<some_type>
Page: 748
Chapter: 26
Section: 3.2
Variables: template <typename T>
class C
Some instance I of C<some_type>
Context: C++

Question: Best practice with template
Answer: limit as much as possible dependency to context.
Page: 748
Chapter: 26
Section: 3.2
Context: C++

Name: Argument dependency lookup
Definition: the fact that an instantiation of f<T> or C<T> uses the namespace of T for lookup
Page: 748
Chapter: 26
Section: 3.2
Context: C++
Example: endl is looked up in std
Abbreviation: ADL
Applied to: std::cout << "Hello, world" << endl;

Name: Compite-time polymorphism
Definition: Polymorphism implemented with template
Page: 760
Chapter: 27
Section: 1
Context: C++
Name2: Parametric polymorphism
Intuition: contrary to run-time polymorphism where the actual type of an object determines the method called using vtable

Question: When is template better than inheritance
Answer: If the type of a method of a function must vary
Page: 762
Chapter: 27
Section: 1
Context: C++
Answer2: when explicit use of free store is undesirable
Answer3: If run-time efficiency is at such a premium that inlining of operations is essential

Question: When is inheritance better than hierarchy
Answer: if the actual type of object used cannot be known at compile time
Page: 762
Chapter: 27
Section: 1
Context: C++
Answer2: If a hierarchical relationship is required between generated or derived types; as it allow conversions without making the explicit

Question: How to give Base :> Derived hierarchy into a Class<Base> :> Class<Derived> hierarchy
Answer: create an implicit conversion operator from Class<A> to Class<B> that uses the conversion from A to B. It can be instantiated only if A to B exists
Page: 765
Chapter: 27
Section: 2.2
Context: C++
Answer2: Write an explicit conversion from Class<B> to Class<A> which fails if B's instance can't be converted explictly to A