Home > Backend Development > C++ > What are Template Template Parameters and Why Are Template Template Templates Not Possible in C ?

What are Template Template Parameters and Why Are Template Template Templates Not Possible in C ?

Patricia Arquette
Release: 2024-11-02 09:10:03
Original
272 people have browsed it

What are Template Template Parameters and Why Are Template Template Templates Not Possible in C  ?

Template Template Parameters: Unveiling the Mysteries

In the realm of template programming, the concept of template template parameters can seem like an enigma. Attempting to comprehend them may leave you feeling lost, like being entangled in an intricate puzzle.

Imagine yourself encountering the following code snippet:

<code class="cpp">template<template<class X> class Z = B>
class BB{};</code>
Copy after login

Here, the parameter list of the templated class BB includes the following line:

<code class="cpp">template<class X> class Z = B</code>
Copy after login

The problem lies in distinguishing between the Z that appears within the parameter list (i.e., the template template parameter) and the hypothetical class Z defined by the template.

Understanding the Distinction

Template template parameters are similar to ordinary template type parameters, but they encompass templates rather than concrete types.

For instance, consider the following code:

<code class="cpp">template <typename Type>
class Foo {
    Type m_member;
};

template <template <typename Type> class TemplateType>
class Bar {
    TemplateType<int> m_ints;
};</code>
Copy after login

Here, Foo is a simple template class that accepts a type parameter, while Bar is a template template class that accepts a template that takes a type parameter.

Parallels to Function Pointers

Template template parameters can be likened to function pointers. Regular functions accept value-like arguments, just as regular templates accept types. However, there are higher-order functions that accept function pointers receiving value-like arguments, analogous to template templates accepting templates that accept types.

Why Template Template Templates Are Not Possible

While template templates certainly exist, template template templates are not a valid concept in C . The standardization committee deliberately chose to limit template recursion to one level out of implementation concerns. However, this decision does not prevent the possibility of introducing them in the future.

Far-fetched but Intriguing Applications

Although rare in practice, template template templates have potential use cases. One such example lies in designing a highly generic algorithm for graph search. The algorithm could be written to operate on a generic data structure, such as a stack or queue, and accommodate various underlying implementation containers. This would allow for the creation of custom graph search algorithms through type substitutions, such as:

<code class="cpp">search<Stack, Vector>( myGraph ); // DFS
search<Queue, Deque>( myGraph ); // BFS</code>
Copy after login

Conclusion

Understanding template template parameters requires shifting your perspective toward templates as higher-order constructs. While they may not be encountered frequently, they provide immense flexibility for designing sophisticated solutions to complex programming problems.

The above is the detailed content of What are Template Template Parameters and Why Are Template Template Templates Not Possible in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template