templates

  1. Template Functions:

    • Function templates allow you to write generic functions that can work with different data types.
    • The template parameters are specified using angle brackets (<>) when defining and calling the function.
    • Example:
      template <typename T>
      T add(T a, T b) {
          return a + b;
      }
      
  2. Template Classes:

    • Class templates allow you to create generic classes that can work with different data types.
    • The template parameters are specified using angle brackets (<>) when defining and creating instances of the class.
    • Example:
      template <typename T>
      class Stack {
      public:
          void push(const T& item) { container.push_back(item); }
          // ...
      private:
          std::vector<T> container;
      };
      
  3. Template Parameters:

    • Template parameters can be of different types:
      • Type parameters (using typename or class)
      • Non-type parameters (e.g., integral types, pointers, or references)
    • Example:
      template <typename T, int N>
      class FixedArray {
      public:
          T data[N];
          // ...
      };
      
  4. Default Template Arguments:

    • You can provide default values for template parameters, similar to function parameters.
    • This allows you to create more flexible and reusable templates.
    • Example:
      template <typename T, typename Allocator = std::allocator<T>>
      class Vector {
      public:
          // ...
      };
      
  5. Template Specialization:

    • Allows you to provide a specific implementation of a template for a particular set of template arguments.
    • This can be used to optimize or customize the behavior of a template for specific data types.
    • Example:
      template <typename T>
      struct MyStruct {
          T data;
          // ...
      };
      
      template <>
      struct MyStruct<int> {
          int data;
          // Specialized implementation for int
      };
      
  6. Variadic Templates:

    • Introduced in C++11, variadic templates allow you to create functions and class templates that can accept an arbitrary number of type arguments.
    • This feature enables the creation of powerful generic programming constructs, such as std::tuple and std::make_unique.
    • Example:
      template <typename... Args>
      void print(Args... args) {
          ((std::cout << args << " "), ...) << std::endl;
      }
      
  7. Template Aliases (using Declarations):

    • Introduced in C++11, template aliases allow you to create a new name for an existing template.
    • This can be useful for improving code readability and reducing duplication.
    • Example:
      template <typename T, std::size_t N>
      using Array = std::array<T, N>;
      
  8. Trailing Return Type:

    • Introduced in C++11, the trailing return type syntax allows you to define the return type of a function template after the parameter list.
    • This can be particularly useful for complex return types or when using auto as the return type.
    • Example:
      template <typename T, typename U>
      auto add(T a, U b) -> decltype(a + b) {
          return a + b;
      }
      
  9. Fold Expressions:

    • Introduced in C++17 (but related to C++11 variadic templates), fold expressions provide a concise way to perform operations on the elements of a parameter pack.
    • Fold expressions can be used to implement various generic algorithms and utility functions.
    • Example:
      template <typename... Args>
      auto sum(Args... args) {
          return (... + args);
      }
      
  10. Template Argument Deduction for Class Templates:

    • Introduced in C++17 (but related to C++11 features), this feature allows for automatic deduction of template arguments when creating instances of class templates.
    • This can help reduce code duplication and improve readability.
    • Example:
      template <typename T, typename U>
      class Pair {
      public:
          Pair(T t, U u) : first(t), second(u) {}
          T first;
          U second;
      };
      
      Pair pair = Pair(42, "hello");  // Deduced as Pair<int, const char*>
      

These are the key C++11 template features that allow you to write flexible, reusable, and powerful C++ code. By understanding and applying these concepts, you can create robust and efficient generic programming solutions.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容