Understanding Template Template Parameters
In the realm of C , template template parameters present a challenge for some programmers. Let's dispel a common misconception:
Misconception:
In the code snippet below, what prevents C from considering Z as another templated class rather than a templated parameter?
<code class="cpp">template<class T> class B {}; // Templated class template<template<class X> class Z = B> // Line in question class BB{};</code>
Explanation:
Template template parameters behave similarly to normal template type parameters. However, they do not accept concrete types; instead, they accept templates themselves. Here's a simplified understanding:
In the example, the syntax template class Z = B> defines a templated type parameter Z that expects a template class with one template parameter X.
Template Template Template Parameters (Not Possible)
It's important to note that template template template parameters are not possible in C . This is a design decision made by the standardization committee to simplify compiler implementation.
Analogy: Functions and Function Pointers
For a better understanding, draw parallels with functions and function pointers:
Similarly, template template parameters accept templates (arguments) that accept arguments.
Example and Use Case
Although template template template parameters are not possible, let's imagine a situation where they might be useful:
Suppose we want to create a generic graph searching library with algorithms that operate on different types of stacks or queues, which in turn can be implemented with different container types. A template template template parameter could enable us to specify all these aspects in a single function call. However, since template template template parameters are not available, this use case remains a hypothetical example.
The above is the detailed content of Why Can\'t C Handle Template Template Template Parameters?. For more information, please follow other related articles on the PHP Chinese website!