Home > Backend Development > C++ > body text

Why Can\'t C Handle Template Template Template Parameters?

Mary-Kate Olsen
Release: 2024-11-02 19:08:31
Original
751 people have browsed it

Why Can't C   Handle Template Template Template Parameters?

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>
Copy after login

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:

  • Template Type Parameters: Match concrete types (e.g., int, double, Foo, etc.)
  • Template Template Parameters: Match templates (i.e., classes or structs with template parameters)

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:

  • Functions: Accept values as arguments
  • Function Pointers: Accept functions (arguments) that accept values

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!

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