Best Practices for Naming Template Parameters

What are the best practices for naming template parameters?

Naming template parameters is an important aspect of writing clear and maintainable C++ code. While there aren't strict rules, there are some conventions that may be worth considering:

Single-Letter Names for Simple Cases

For simple, general-purpose templates, it's common to use single uppercase letters. For example:

  • T for a general type
  • N for a compile-time integral value
  • E for element type in containers
template <typename T, std::size_t N>
class Array {
  T elements[N];
};

Descriptive Names for Complex Cases

For more complex templates or when the purpose of the parameter is not immediately obvious, use more descriptive names:

#include <memory> // For std::allocator

template <
  typename ElementType,
  typename Allocator = std::allocator<ElementType>
>
class MyVector {
  // ...
};

Prefixes for Different Kinds of Parameters

  • T for type parameters: TKey, TValue
  • N for non-type parameters: NSize, NAlignment
#include <cstddef> // For std::size_t

template <
  typename TKey,
  typename TValue, std::size_t NMaxSize
>
class FixedMap {
  // ...
};

Casing

It's common to use PascalCase for type parameters and camelCase for non-type parameters:

#include <cstddef> // For std::size_t

template <
  typename InputIterator,
  typename OutputIterator,
  std::size_t bufferSize
>
void CopyWithBuffer(
  InputIterator first,
  InputIterator last,
  OutputIterator result) {
  // ...
}

Consistency with the Standard Library

If your template is similar to something in the standard library, consider using similar naming conventions:

#include <utility> // For std::less

template <
  typename Key,
  typename T,
  typename Compare = std::less<Key>
>
class MyOrderedMap {
  // ...
};

Use typename Instead of class

While both typename and class are allowed, typename is generally preferred as it's more accurate (not all types are classes):

template <typename T>
class MyTemplate {
  // ...
};

Remember, the goal is to make your code as readable and self-explanatory as possible. Choose names that convey the purpose or constraints of the parameter. If a single letter suffices, use it. If more context is needed, don't hesitate to use a longer, more descriptive name.

Lastly, be consistent within your codebase or team. If your team has established naming conventions, follow those even if they differ slightly from these general guidelines.

Class Templates

Learn how templates can be used to create multiple classes from a single blueprint

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Partial Specialization of Class Templates
Is it possible to partially specialize a class template?
Compile-Time Polymorphism with Templates
Can I use templates to implement compile-time polymorphism?
Creating Templates with Variable Number of Parameters
How can I create a template that works with an arbitrary number of type parameters?
Default Values for Non-Type Template Parameters
Is it possible to have default values for non-type template parameters?
Constraining Template Arguments
How can I create a template that only accepts certain types of arguments?
Typename vs Class in Template Declarations
What's the difference between typename and class in template parameter declarations?
Creating Class Templates with Multiple Types
How can I create a class template that works with both primitive types and user-defined types?
Specializing Class Templates
How can I specialize a class template for specific types?
Using Template Parameters in Constructors
Can I use template parameters in the constructor of a class template?
Template Methods in Non-Template Classes
How do I create a template method within a non-template class?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant