typename vs class

Scotty Moe

Updated on:

This article explores the difference between the keywords ‘typename’ and ‘class’ in templates.

In the context of dependent types, the keyword ‘typename’ is employed to reference a nested type that is contingent upon another template parameter, while ‘class’ is used to specify a template template.

However, both keywords are permissible in the case of template template since C++17.

It is noteworthy that ‘class’ must be used when explicitly instantiating a template.

Furthermore, there are specific instances where ‘typename’ and ‘class’ are not interchangeable. In particular, ‘typename’ is utilized to declare dependent types, whereas ‘class’ is employed in template template and explicit instantiation scenarios.

It is important to emphasize that the choice between ‘typename’ and ‘class’ is a matter of style and is not mandated by the standard. While both keywords can typically be used interchangeably, it is recommended to opt for ‘typename’ when declaring a nested type that depends on another template parameter, as it offers a clearer description of its purpose.

Ultimately, the decision to employ ‘typename’ or ‘class’ should be consistently applied throughout the codebase and is contingent upon personal preference and coding style.

Definition

The definition of the difference between the keywords ‘typename’ and ‘class’ in templates is an important aspect to understand in order to correctly use them in dependent types and template templates, respectively.

In the case of dependent types, ‘typename’ is used when referencing a nested type that depends on another template parameter. It is used to declare dependent types and hints that a dependent type is being referred to.

On the other hand, ‘class’ is used when specifying a template template. It is used in templates that accept any type, including built-ins, and in template templates. It is also used when explicitly instantiating a template.

While ‘typename’ and ‘class’ can be used interchangeably in most cases, there are specific cases where they are not interchangeable.

The choice between ‘typename’ and ‘class’ is a matter of style and personal preference and does not affect the functionality of the template.

Use in Dependent Types

When referencing a nested type that relies on another template parameter, the use of either ‘typename’ or ‘class’ is necessary in order to specify a dependent type.

The keyword ‘typename’ is used to declare a dependent type and hints to the compiler that a name refers to a type. It is used when the compiler needs to determine if a name refers to a type and when a name is dependent on a template parameter.

On the other hand, the keyword ‘class’ is used in templates that accept any type, including built-ins, and in templates that only work with real classes.

Both ‘typename’ and ‘class’ can be used interchangeably in most cases, depending on personal preference and coding style.

Use in Template Templates

In the case of template templates, the choice between ‘typename’ and ‘class’ is determined by the specific requirements of the template and the desired functionality.

The keyword ‘typename’ is used when declaring a nested type that depends on another template parameter within the template template. It is used to explicitly indicate that a dependent type is being referred to.

On the other hand, the keyword ‘class’ is used when specifying a template template parameter. It is used to define a template parameter that accepts any type, including built-in types.

Since C++17, both ‘typename’ and ‘class’ are allowed in the case of template templates.

The decision to use either ‘typename’ or ‘class’ in template templates should be based on the intended behavior and purpose of the template.

Leave a Comment