Home  >  Article  >  Backend Development  >  Why are Default Template Arguments Restricted to Class Templates in C ?

Why are Default Template Arguments Restricted to Class Templates in C ?

Susan Sarandon
Susan SarandonOriginal
2024-11-01 17:29:30457browse

Why are Default Template Arguments Restricted to Class Templates in C  ?

Why Default Template Arguments are Restricted to Class Templates

In C , default template arguments are only allowed on class templates but not on function templates. This restriction may seem surprising, but there are several reasons behind it.

Default Template Arguments for Class Templates

Default template arguments allow class templates to provide a default value for certain parameters. This can be useful when the default value is unlikely to change for most instances of the template. For example, a class template for a sorting algorithm could have a default template argument for the comparison function used in sorting.

Restriction for Function Templates

However, default template arguments are not allowed for function templates because they would introduce ambiguity. Consider the following hypothetical example:

<code class="cpp">struct my_class {
  template<class T = int>
  void mymember(T* vec) {
    // ...
  }
};</code>

In this example, it would be unclear whether T is the default type for the mymember function or for the my_class template itself. This ambiguity could lead to subtle errors in the code.

Advent of C 11

Prior to C 11, the prohibition of default template arguments for function templates was a significant limitation. However, C 11 introduced a new feature called "type aliases" which allows us to define aliases for types. This provides a workaround for the lack of default template arguments for function templates. For example, the example above could be rewritten as follows:

<code class="cpp">struct my_class {
  typedef int default_type;
  template<class T = default_type>
  void mymember(T* vec) {
    // ...
  }
};</code>

While type aliases are not as convenient as default template arguments, they provide a similar functionality and allow us to overcome the limitations of the current C standard.

Bjarne Stroustrup's Perspective

In a defect report, Bjarne Stroustrup, the original designer of C , expressed his view on the prohibition of default template arguments for function templates:

The prohibition of default template arguments for function templates is a misbegotten remnant of the time where freestanding functions were treated as second class citizens and required all template arguments to be deduced from the function arguments rather than specified.

The above is the detailed content of Why are Default Template Arguments Restricted to Class Templates in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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