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.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容